TDictionary, how to destroy it
I have tried every way to Destroy TDictionary
but whenever I have the ReportMemoryLeaksOnShutdown
active it always leaves something without being destroyed.
I've tried running the list and destroying item by item.
Code:
with foDict.GetEnumerator do
while MoveNext do
Current.Value.Free;
foDict.Free;
As I tried to also destroy only the foDict.Free;
Both memory leaks. Does anyone have any suggestions?
3 answers
Using your example, the code would have the following changes:
New Class:
TMyCustomDictionary = TObjectDictionary<integer, TCliente>;
New use of foDict object:
foDict: TMyCustomDictionary;
New Create of foDict object
foDict := TMyCustomDictionary.Create([doOwnsValues]);
Once this is done, the button FreeList
, becomes no longer useful
Tests as follows::
destructor TMemCorrectedDictionary.Destroy;
begin
inherited;
Values.Free;
Keys.Free;
end;
More information here: Memory Leak in TDictionary-Problems with Workaround?
Yes, with TObjectDictionary you can use .Create ([doOwnsValues]) that free all objects automatically. [doOwnsValues] is not possible to TDictionary, you need to do manually.