On Thursday 29 October 2009 14:00:53 Graeme Geldenhuys 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); > end; > > > I'm busy converting a old program from OS/2 days that used PChar > exensively. I'm slowly but surely converting this code to AnsiString. > But as a quick fix, am I allowed to do what is shown above? Without > causing memory leaks or something else?
Text points to garbage after AppendText() because s will be freed at end of AppendText(). Martin _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal