Delphi - line structures are not freed [FastMM manager]

If I announce

PSomeStruct = ^TSomeStruct; TSomeStruct = record s1 : string; end; 

and I run the following code:

 var p: PSomeStruct; begin new(p); p^.s1:= 'something bla bla bla'; dispose(p); 

The FastMM 4 memory manager reports that a memory leak has occurred (type: string, data dump: "something bla bla bla"). However, if I set the string s1 to empty before calling dispose , that's OK.

The second way I found is to switch from a record type to a class, and instead of new create an instance, and instead of dispose I call instance.Free() . It works without manually clearing strings.

Is there a way to get Delphi to automatically clear my lines when dispose called?

+4
source share
2 answers

Is FastMM the first module used in your .dpr? Otherwise, it may be completed too early, reporting false memory loss.

And does this simplified code also make the same memoryleak as when using JvSimpleXML? When this is not the case, more likely to happen than you suspect.

In my opinion: when FastMM reports a memory leak, there is memory.

+1
source

You are already doing the right thing. If FastMM says that the line has leaked, then FastMM is wrong or reports another line from the one you think. The Dispose procedure releases rows from records.

In this particular case, in any case, no memory should have been allocated for this row. This is a string literal, so I expect the compiler to assign this literal; its reference count should be -1 , and FastMM should never have seen it.

+1
source

All Articles