PureBasic vs. PowerBasic (Bob's Opinion)

Started by Bob Houle, February 16, 2016, 04:41:42 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

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


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


<long variable> = RemoveAny( s1$, s2$ )
PeekS <long variable>
SysFreeString <long variable>


David Roberts

#31
Quotetreats 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.

<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.

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

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é.