Memory leaks in WPF / C #

Note that this is done in WPF/C# and not in .net2.0 Winforms

I have a ListBox that contains say Class X objects. Class X contains a BitmapSource object that is displayed in the list, so it is displayed similarly to [Image] [Text]

This is loaded using CreateBitmapSourceFromHBitmap - note also that I call DeleteHBitmap to remove the HBitmap descriptor during this call, which is well known from the messages I saw on Google / etc.

I have a tree containing the specified ListBox in each TreeViewItem - usually the tree has several loaded items. Users can drag and drop these images into different TreeViewItems. To manage these operations, I manually call the operations:

 <code> ItemCollection.RemoveAt </code> <code> ItemCollection.Insert </code> 

to move images from the collection of ListBox elements, note that when I insert, I create a new Class X object to be inserted into the collection of ListBox elements

I noticed that I get a constant memory leak when calling such operations several times, for 5-10 minutes of constant drag and drop.

My question is:

Am I managing the BitmapSource traffic BitmapSource ? Is there something I'm doing to not completely remove the images from the ItemCollection ?

Or is there something fundamental that I missed?

+6
c # wpf
source share
1 answer

What is the definition of a variable that contains the image in you ClassX ??? The problem may be that you are creating a new ClassX and the old one is not deleted by the GC, as the head has two different instances of ClassX.

Since you are using unlisted code (CreateBitmapSourceFromHBitmap), you should check to see if the entire finalize method is called correctly (albeit closed or deleted) and that there are no static links that may point to your ClassX.

Remember that if ClassX is not deleted, the Bitmap instance will be available on the graph created by GC so as not to remove it from the heap.

I recommended using perfmon and adding a .Net memory object to find out if there is any object that survived, a terminating or pinned object, these are the ones you are probably interested in memory leak.

Hope this helps: P, but it would be better if you put ClassX code.

+1
source share

All Articles