Tdictionary, how to destroy it

Asked

Viewed 633 times

2

I’ve tried every way to destroy TDictionary but whenever I have the ReportMemoryLeaksOnShutdown Active he always leaves something untouched. I’ve tried to run the list and destroy item by item.

Code:

with foDict.GetEnumerator do
  while MoveNext do
    Current.Value.Free;

foDict.Free;

As I tried to destroy only the foDict.Free;

Both there is memory leakage. Someone there has some suggestion?

  • Is this just your code where you use foDict? @Elanduartefraga

  • problem is just to give him a free pass. foDict: Tdictionary<integer, Tcliente>; It works perfectly but when releasing it from memory always gets some "garbage".

  • tried my answer? @Elanduartefraga

  • I didn’t see how to test your answer but I created the project to better explain https://dl.dropboxusercontent.com/u/77323830/TDictionary.rar

  • When you create a Dictionary, the best way to ensure that it will be released is to use the WnsValue parameter in create: myDictionary<TKey,TValue>.Create([doOwnsValues])

  • And ai @Elanduartefraga , some advance ?

  • @Elanduartefraga if I could help with my answer, you can accept the answer by clicking on the left side of it. If you need any more help, let us know

Show 2 more comments

3 answers

2

Using your example, the code would have the following changes:

New class:

TMyCustomDictionary = TObjectDictionary<integer, TCliente>;

New use of the object foDict:

foDict: TMyCustomDictionary;

New create of the foDict object

foDict := TMyCustomDictionary.Create([doOwnsValues]);

Done that, the button FreeList, is no longer useful

0

-2

Yes, with Tobjectdictionary you can use . Create([doOwnsValues]) that free all Objects Automatically. [doOwnsValues] is not possible to Tdictionary, you need to do Manually.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.