I have problems understanding pointer behavior. I have a very simple example:
type
PSL = ^TStringList;
...
var
myPSL : PSL;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
New(myPSL);
myPSL^ := TStringList.Create;
myPSL^.Add('String 1');
myPSL^.Add('String 2');
myPSL^.Add('String 3');
end;
...
procedure TForm1.FormDestroy(Sender: TObject);
begin
Dispose(myPSL);
end;
With this code I get a memory leak report
29 - 36 bytes: UnicodeString x 3
37 - 44 bytes: Unknown x 1
85 - 92 bytes: TStringList x 1
Now if i call
myPSL^.Free;
before deleting the pointer, then nothing is reported.
I do not understand why this is happening. I know that calling New () allocates enough memory (depending on the type of pointer), and calling Dispose () takes care to free the same memory, then why do I need a Free pointer, as if it were a "real" object?
Thank!
source
share