You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a base class that defines append list and derived class that have a method that allocates new items and adds it to the list.
However, after allocating the class on stack and calling that method, after a few seconds GC detects memory leaks, even though the list field has a destructor that deletes all items.
publicstructA:IDisposable{publicList<float>list=new.();publicvoidDispose(){deletethis.list;}}publicstructB:A{}publicclassC{publicBb=.();
public ~this(){this.b.Dispose();}}static{publicstaticvoid Main(){
C c =new.();
Console.Write("Sleeping... ");
System.Threading.Thread.Sleep(3000);// Crashes with a memory leak (on A's list) before reaching this call.
Console.WriteLine("done!");deletec;}}
Some additional oddities:
Sleeping for shorter periods of time avoids the crash. From experimentation, 2000 ms seems to be about the threshold where the memory leak stops.
Removing the firstConsole.Write avoids the crash, even when sleeping for much longer periods of time (say, 10 seconds or longer).
Using an A instance within C (rather than a B instance) avoids the crash:
publicclassC{// Doesn't leak!publicAa=.();
public ~this(){this.a.Dispose();}}
ComposingA within B rather than inheriting avoids the crash:
I have a base class that defines append list and derived class that have a method that allocates new items and adds it to the list.
However, after allocating the class on stack and calling that method, after a few seconds GC detects memory leaks, even though the list field has a destructor that deletes all items.
Code example
Output:
Example project
The text was updated successfully, but these errors were encountered: