1

I got a memory leak problem with my code, using images with VB.NET and WPF. Even if I dispose my bitmap objects, the memory doesn't stop growing.

Here is my code

Dim imageB As New ImageBrush                
imageB.ImageSource = Me.GetImageSource(My.Resources.checkImage)
My.Resources.checkImage.Dispose()
Me.mainCanvas.Background = imageB

and here is the method GetImageSource():

Public Function GetImageSource(ByVal bmp As System.Drawing.Bitmap) As ImageSource
    Dim imageSource As BitmapSource
    Dim hbitmap As IntPtr = bmp.GetHbitmap
    Dim sizeOptions As BitmapSizeOptions = BitmapSizeOptions.FromEmptyOptions()
    imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions)
    ImageSource.Freeze()
    DeleteObject(hbitmap)
    bmp.Dispose()
    Return imageSource
End Function

Any ideas ? Thanks.

Elmo
  • 6,409
  • 16
  • 72
  • 140
Superzadeh
  • 1,106
  • 7
  • 23
  • 2
    You know that the CLR doesn't release memory it has allocated immediately, right? Right? –  Jan 12 '12 at 14:50
  • How do you know you have a memory leak? i. e. what tool are you using to make this determination? Task Manager is notoriously poor for determining this. Assuming there is a memory leak, be sure that you dispose, beside images and bitmaps, but brushes and pens, etc. Make liberal use of the using statement where applicable. – Chris Dunaway Jan 12 '12 at 15:08
  • possible duplicate of [How to release the occupied memory](http://stackoverflow.com/questions/5191897/how-to-release-the-occupied-memory) Focus on [this answer](http://stackoverflow.com/a/5192350/366904), not the accepted one with 0 votes... – Cody Gray - on strike Jan 12 '12 at 15:20
  • @Will Yes I know, I even tried to call GC.Collect and as expected, it didn't improve at all. @ChrisDunaway I know the memory leak is exactly in these lines since as soon as I comment this line `imageB.ImageSource = Me.GetImageSource(My.Resources.checkImage)`, the memory leak is gone. – Superzadeh Jan 12 '12 at 15:59
  • @cahmadzadeh: You getting an OOM exception or are you just watching task manager? –  Jan 12 '12 at 16:00
  • 1
    @Will Watching task manager and with enough time (having stress unit tests running), GDI+ exceptions starts showing up. – Superzadeh Jan 12 '12 at 16:03
  • @cahmadzadeh: What kind of errors? –  Jan 12 '12 at 16:04
  • If you're getting GDI+ exceptions, then you're *not* calling `Dispose` on all of the objects you're using. Task Manager, however, is completely meaningless. Don't use it to debug memory leaks, or you'll go insane. Wrap all objects that have a `Dispose` method in a `using` block and these problems should go away. – Cody Gray - on strike Jan 12 '12 at 16:11
  • Generic GDI+ Error kind of exception – Superzadeh Jan 12 '12 at 16:12
  • 2
    DeleteObject() returns a BOOL. Don't ignore it. – Hans Passant Jan 12 '12 at 16:34
  • I found where was the problem, the developer was somehow keeping mainCanvas in the visual tree, and adding them programmatically (not through binding) was making the memory grow and grow. The solution was to implement an IDisposable method on the parent of this canvas and calling the Clear() method on its children when we needed to get rid of it. Thanks anyway for your help. @HansPassant I just removed that code for simplicity purporse but thanks for noticing. – Superzadeh Jan 12 '12 at 16:54

0 Answers0