RAD Studio |  |

Returning a PChar Local Variable
See Also
Collapse All

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:

Copy Code

    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.

See Also

String Dependencies

Passing a Local Variable as a PChar

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!