lockedscope
lockedscope

Reputation: 983

Why would a routine need a PChar(type-cast from a string) passed to it remain in memory after it returns?

var
  S: string;
begin
  SetLength(S, MAX_SIZE);// when casting to a PChar, be sure the string is not empty
  SetLength(S, GetModuleFilename(0, PChar(S), Length(S)));
  // statements
end;

To eliminate the overhead of copying the buffer, you can cast the string to a PChar (if you are certain that the routine does not need the PChar to remain in memory). http://docwiki.embarcadero.com/RADStudio/en/Passing_a_Local_Variable_as_a_PChar

We have a string and it will remain in memory until its reference count decremented to 0, so it will not be removed in the course of scope. So, why do we need PChar to remain in memory? Is there some API function that require the exact same PChar reference that is passed to another API function previously?

Especially i am considering the sample code. So, the question must be "why would a routine need a PChar(type-cast from a string) passed to it remain in memory after it returns?". Async i/o routines, or an async method that access the passed pchar after the caller returns, or modify of a global string by another thread are good reasons.

Upvotes: 2

Views: 518

Answers (1)

CodesInChaos
CodesInChaos

Reputation: 108790

Sometimes the API stores the pointer until a later time. For example think of async IO.

Or if you wrote code like the following, it'd fail too:

function Broken():PChar;
var s:string;
begin
  s:=IntToStr(Random(100));//no string constant
  return PChar(s);
end;

One important point is if you cast a string with refcount>1 to PChar, it will create a copy, and then hand you a string with refcount=1. So your PChar becomes invalid once the s you passed in becomes invalid, even if it had a refcount>1.

Another broken example:

var p:PChar;
var s:string;

s := IntToStr(Random(100));//no string constant
p := PChar(s);
s := "ABC";
DoSomething(p);//p is invalid by now

Upvotes: 6

Related Questions