Show/Hide Toolbars

XSharp

The compiler generated an automatic conversion to PSZ. This will create a memory leak in your application. Please use String2Psz() to let the compiler manage the lifetime of the PSZ or use StringAlloc() and memory the lifetime of the PSZ yourself.

 

This may happen in code like this:

FUNCTION Start() AS VOID
LOCAL uValue AS USUAL
uValue := "SomeString"
? Test(uValue)
? Test(PSZ(_CAST, "AnotherString"))
RETURN
 
FUNCTION Test(p AS PSZ) AS STRING
RETURN Psz2String(p)

The compiler detects that the Test() function wants to receive a Psz() and creates a conversion from USUAL to PSZ on the fly. When the contents of the usual is a Pointer then no memory will be allocated. When the contents of the usual is a string then memory will be allocated that will hold the PSZ after the conversion from Unicode to Ansi.
The compiler does not know what the Test() function will do with the PSZ and can't therefor control the lifetime of the variable. We recommend that you change the code to either use String2Psz() or StringAlloc().
Of course these functions will only work if the usual contains a string like in the example above.
 

FUNCTION Start() AS VOID
LOCAL uValue AS USUAL
uValue := "SomeString"
? Test(String2Psz(uValue))
? Test(String2Psz("AnotherString"))
RETURN
 
FUNCTION Test(p AS PSZ) AS STRING
RETURN Psz2String(p)
 

or

FUNCTION Start() AS VOID
LOCAL uValue AS USUAL
LOCAL p as PSZ
uValue := "SomeString"
p := StringAlloc(uValue)
? Test(p)
MemFree(p)
p := StringAlloc("AnotherString")
? Test(p)
MemFree(p)
RETURN
 
FUNCTION Test(p AS PSZ) AS STRING
RETURN Psz2String(p)