From: keeper@mindspring.com (Mark Johnson)
Subject: Re: [delphi] TMemo text
Date: Thu, 10 Aug 1995 17:21:26 -0500


>> lpVar := StrAlloc(SizeOf(Memo.Lines.GetText) + 1);
>> lpVar := Memo.Lines.GetText;

This is incorrect and will result in lots of lost resources.
Although, you will get the text this way...

>   GetMem(lpVar, 32768);
>   StrCopy(lpVar, Memo.Lines.GetText);
>   ...
>   FreeMem(lpVar, 32768);

This is also incorrect and will also result in lost resources.
Although, you will also get the text this way...

The correct usage of TMemo.Lines.GetText() (or any other TString's GetText()
method) is as follows:

  var
    lpVar : PChar;
  begin
    lpVar := Memo.Lines.GetText;
    try
      {do whatever you like with/to lpVar's contents}
    finally
      StrDispose(lpVar);
    end;
  end;

The GetText method creates a copy of the text in Memo.Lines (or other
TStrings object) via the StrAlloc() function.  It is entirely up to
you, the programmer, to dispose of the PChar when you are done via
the StrDispose() function.  Since GetText returns a copy of the
text, you can muck about with its contents as you please without
modifying the text in the TMemo.

Wouldn't it be nice if the on-line documentation told us this stuff?
(Lesson:  Buy the VCL source today! ;)

--Mark Johnson
______________________________________________________________________________
 Mark R. Johnson       _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_
 Atlanta, GA USA       _/  http://www.mindspring.com/~cityzoo/cityzoo.html  _/
 keeper@mindspring.com _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
