Show/Hide Toolbars

XSharp

String2Psz() and Cast2Psz() are only allowed in a local context:

 

the result of String2Psz() or Cast2Psz() cannot be persisted in a memory location that does not have a local scope. T

he PSZ value is destroyed when the scope in which it is allocated ends.

You have to use StringAlloc() instead and managed the lifetime of the PSZ in code.

 

Example

In the code below the p2 field is inline initialized with String2Psz(). this is not allowed.

Also in the constructor we try to assign the value of p1 with String2Psz(). This is also not allowed.

 
CLASS TestClass
  EXPORT p1 AS PSZ
  EXPORT p2 := String2Psz("psz2") AS PSZ // Error here
 
  CONSTRUCTOR()
     p1 := String2Psz("psz1") // Error here
  RETURN
END CLASS

 

 

The solution is something like this

 

CLASS TestClass
  EXPORT p1 AS PSZ
  EXPORT p2 AS PSZ
 
  CONSTRUCTOR()
     p1 := StringAlloc("psz1")
     p2 := StringAlloc("psz2")
  DESTRUCTOR()
     MemFree(p1)
     MemFree(p2)
 
 
  RETURN
END CLASS