The line PTitle := PChar(WTitle) does not copy the string data from WTitle
to PTitle, it ensures that WTitle is null-terminated and then assigned
@(WTitle[1]) to PTitle.

If you want to have a PChar reference to a string, just PTitle :=
PChar(WTitle) is fine and you don't need to GetMem or FreeMem at all.

If you want a copy of WTitle in a PChar to work on then change the PTitle :=
PChar(WTitle) line to read StrCopy(PTitle, WTitle).  (And leave the GetMem,
FreeMem as it is)

----- Original Message -----
From: Patrick Dunford <[EMAIL PROTECTED]>
To: Multiple recipients of list delphi <[EMAIL PROTECTED]>
Sent: Friday, January 21, 2000 5:37 PM
Subject: [DUG]: Invalid pointer operation


> I have some code that looks like this in my program:
>
> var PTitle: PChar;
>
> [...]
>
>      if Trim(WTitle)<>'' then
>      begin
>           GetMem(PTitle,255);
>           PTitle:=PChar(WTitle)
>      end
>      else PTitle:=nil;
>
> [...]
>
>      if PTitle<>nil then
>         FreeMem(PTitle);
>
> The application is calling this code with WTitle containing a string. It
> goes alright up to the FreeMem statement when it throws "Invalid pointer
> operation".
>
> Is this supposed to be written some other way? I want to pass a nil
pointer
> in the FindWindow call or the pointer to a window title if passed in
WTitle
> (declared as PChar)
>
> ============================================
> Patrick Dunford, Christchurch, NZ
> EnzedRailWeb Sites
> http://patrick.dunford.com/enzedrailweb/
>
> --------------------------------------------------------------------------
-
>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to