Author Topic: PureBasic vs. PowerBasic (Bob's Opinion)  (Read 6469 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 3123
  • Gender: Male
    • José Roca Software
Re: PureBasic vs. PowerBasic (Bob's Opinion)
« Reply #30 on: November 03, 2016, 04:58:43 AM »
Regarding the p-bstr pseudotype, I only have found that it does an automatic conversion, i.e. it creates a BSTR that is passed to the called function, but nothing about what happens with these temporary bstrings. Maybe it will generate code to release them, but I don't know.

If it is passed by reference:

Quote
When a method returns a string (typically a BSTR one), it asks you to provide a pointer into which it will fill the string. Here no automatic conversion is supported. What you need to do here is pass a pointer to a long variable, which will be filled with the BSTR pointer. Then you can use PeekS() to read the string. Then do not forget SysFreeString() of course.

Example:

If MyObject\SomeMethod(@bstr_sting) = #S_OK
   Debug PeekS (bstr_string ,  -1, #PB_Unicode)
   SysFreeString_(bstr_string)
EndIf

Regarding the BSTR returned by the PowerBasic function, your Pure Basic prototype

Code: [Select]
Prototype.l ProtoRemoveAny( param1.p-bstr, param2.p-bstr )

treats it as a long value, so it doesn't really know if it is a BSTR or not.

Probably you will have to do something like

Code: [Select]
<long variable> = RemoveAny( s1$, s2$ )
PeekS <long variable>
SysFreeString <long variable>

Offline David Roberts

  • Newbie
  • *
  • Posts: 32
Re: PureBasic vs. PowerBasic (Bob's Opinion)
« Reply #31 on: November 03, 2016, 10:14:28 AM »
Quote
treats it as a long value, so it doesn't really know if it is a BSTR or not.

But I do - it is a Wstring.
 
Code: [Select]
<long variable> = RemoveAny( s1$, s2$ )
PeekS <long variable>
SysFreeString <long variable>

If it was a BSTR then the above would cause an access violation because PeekS, defaulting to reading unicode, would attempt to read a BSTR.
 
However, I now take your point about a memory leak.
 
If I changed back to 'Export as String', instead of 'Export as Wstring', then this works.
 
Code: [Select]
ptr.l = RemoveAny( s1$, s2$ )
MessageRequester( "", PeekS( ptr.l, -1, #PB_Ascii ) )
SysFreeString_( ptr.l )

The BSTR is now being read as Ascii and is then freed.

Alternatively, we could have

Code: [Select]
ptr.l = RemoveAny( s1$, s2$ )
s3$ = PeekS( ptr.l, -1, #PB_Ascii )
SysFreeString_( ptr.l )

The BSTR is read correctly and then converted to unicode.

Thanks, José.
 
 
« Last Edit: November 03, 2016, 10:37:55 AM by David Roberts »