Question

Whilst trying to work out why one of our new applications is crashing I've been chasing down memory leaks in Delphi.

I've hooked up the lastest version of FastMM and have been working through the results, but am confused by the following, which I've boiled down to bare bones for sake of brevity.

We have a record defined with 2 string fields. These are assigned from two TEdit boxes, and at the same time we write the data to a TListView. Here is the key code:

procedure TForm1.SetAssignment;
var
   tp: TestPointer;
   SourceTable, SourceColumn: string;
   LI: TListItem;
begin
   SourceTable := Edit1.Text;
   SourceColumn := Edit2.Text;
   LI := lvTest.Items.Add;
   LI.Caption := SourceTable;
   LI.SubItems.Add(SourceColumn);
   new(tp);
   // Leak occurs here
   tp^.SourceTable := SourceTable;
   tp^.SourceField := SourceColumn;
   // No leak if preceding lines are ommitted
   TestList.Add(tp);
end;

The issue seems to be with with the value of SourceTable/SourceColumn, or with the tp^. values.

The TList is being properly cleaned out when we quite: If we comment out the assignments to tp^.SourceTable/tp^.SourceField then there is no memory leak.

Maybe it's just because it's the New Year, but I can't see how I release the SourceTable/SourceColumn...

Was it helpful?

Solution

I expect that your clean up code calls FreeMem rather than Dispose on the pointer. That's the only thing I can imagine that has those symptoms. FreeMem just frees the memory, but Dispose will also finalize the contents before freeing.

When you call Dispose you must supply a correctly typed pointer. For your case you must pass a TestPointer to Dispose.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top