Need to allocate memory before a Delphi string copy?

Posted by Duncan on Stack Overflow See other posts from Stack Overflow or by Duncan
Published on 2010-06-07T20:49:33Z Indexed on 2010/06/07 20:52 UTC
Read the original article Hit count: 187

Filed under:
|
|

Do I need to allocate memory when performing a Delphi string copy?

I've a function which posts a Windows message to another form in my application. It looks something like this:

// Note:  PThreadMessage = ^TThreadMessage; TThreadMessage = String;

function PostMyMessage( aStr : string );
var
  gMsgPtr : PThreadMessage;
  gStrLen : Integer;
begin
  New(gMsgPtr);
  gStrLen := StrLen(PWideChar(aMsg));
  gMsgPtr^ := Copy(aMsg, 0, gStrLen);
  PostMessage(ParentHandle, WM_LOGFILE, aLevel, Integer(gMsgPtr));

  // Prevent Delphi from freeing this memory before consumed.
  LParam(gMsgPtr) := 0;
end;

© Stack Overflow or respective owner

Related posts about delphi

Related posts about string