Fixing memory Leaks, Finalizer and other ugly problems in your .NET code

8. September 2011 07:48 by Mrojas in General  //  Tags: , , , ,   //   Comments (0)

Fixing memory leaks is sometimes hard. Because even if you track which is the instance
that is being retained, is difficult to determine WHERE was it created.

Another problem I have found difficult to tackle is when you get an exception because of
code that was put in a finalizer (yes I know that putting code in a Finalizer doesn’t sound like the
best programming practice but sometime I just have to give maintenance to code that does that).

So, my friend Jose de Jesus showed me a nice tip to identify where are those objects being created.

He told me to do the following:

1. Add an instance variable like:

 

public System.Diagnostics.StackTrace myClassStackTrace = new System.Diagnostics.StackTrace();

2. Just by doing that I can inspect that field and identify where was this particular instance created!.

Cheers

Categories