At Delphi, the runtime memory manager is supposed to handle the strings. Any
PChars are just pointers to those strings I think. When you use a Pchar as a
string, it must be getting some runtime string proxy that keeps that pchar
and has some size calculation function instead of pointing to some classic
string data that have the size at the 1st byte. When the string that serves
as pchar proxy dies it doesn't deallocate the pchar memory, since it didn't
allocate it itself at the first place (it might have been allocated by some
third-party C dll for example using C memory manager, so that dll will have
to deallocate the pchar using that same mem manager)

Just guessing,
George

P.S. there could always be an FPC runtime bug, so you could write some tests
to check this and report as a bug if needed

> > I asked the question for a string array. I see that we can assign a
> > string directly to a pchar, but when I SetLength() such an
> array, do I
> > have to free the PChar string no more used or is it done
> > automatically?
> >
> > var
> >   a: array of PChar;
> > begin
> >   SetLength(a, 4);
> >   a[0] := 'First pchar line';
> >   a[1] := 'Second pchar line';
> >   a[2] := 'Third pchar line';
> >   a[3] := 'Fourth pchar line';
> >   SetLength(a,2);
> >   a[0] := 'Yet another pchar line';
> >   a[1] := 'At last, a pchar line to finish'; end;
> >
> > Is the following causing a memory leak? Do I have to
> SetLength(a, 0)
> > when I'm completely done with 'a' or is it done automatically?
>
> The following test shows a memory increase for the process
> each time it is executed. So I guess that I have a memory
> leak. An array a PChar is not a big deal then.
>
> Is there a peculiar reason to use such an array for the
> DecodeCSVStr function? (Suggested by Michael, but I send the
> question to all those who are interested.)
>
> procedure TMainForm.Button1Click(Sender: TObject); var
>   a: array of PChar;
>   i: Integer;
>   j: Integer;
> begin
>   Randomize;
>   for i := 0 to 10000 do
>   begin
>     SetLength(a, Random(1024));
>     for j := 0 to High(a) do
>       a[j] := 'this is a nice phrase to test teh pchar stuff,
> yeah, the phrase is of fixed length, but I''ve no time to
> waste on this.';
>   end;
> end;




  _____  

avast! Antivirus <http://www.avast.com> : Outbound message clean. 


Virus Database (VPS): 0623-1, 06/06/2006
Tested on: 7/6/2006 9:02:54 ??
avast! - copyright (c) 1988-2006 ALWIL Software.



_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to