[fpc-pascal] PChar -> AnsiString -> PChar = memory leak?
Mattias Gaertner
nc-gaertnma at netcologne.de
Thu Oct 29 14:28:56 CET 2009
On Thu, 29 Oct 2009 13:20:42 +0000
Henry Vermaak <henry.vermaak at gmail.com> wrote:
> 2009/10/29 Mattias Gaertner <nc-gaertnma at netcologne.de>:
> > On Thu, 29 Oct 2009 15:00:53 +0200
> > Graeme Geldenhuys <graemeg.lists at gmail.com> wrote:
> >
> >> Hi,
> >>
> >> Do I create a memory leak if I cast a PChar it a AnsiString. Then
> >> append text to the AnsiString and then cast it back to the original
> >> PChar?
> >>
> >> eg:
> >> var
> >> Text: Pchar; <-- global var containing text.
> >>
> >> procedure AppendText(const AText: string);
> >> var
> >> s: string;
> >> begin
> >> s := Text + AText;
> >> Text := PChar(s);
> >
> > Text:=strnew(PChar(s));
>
> Won't this leak? You'll have to initialise text to null, then use
> strdispose before strnew if text is not null.
Right you are.
I hit the sent button too early.
Mattias
More information about the fpc-pascal
mailing list