A common error when working with PChars is to store a local variable in a data structure, or return it as a value. When your routine ends, the PChar disappears because it is a pointer to memory, and not a reference counted copy of the string. For example:
function title(n: Integer): PChar; var s: string; begin s := Format('title - %d', [n]); Result := PChar(s); // DON'T DO THIS end;
This example returns a pointer to string data that is freed when the title function returns.
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|