.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Memory Leak problem when using bitmap image as tooltip?
 

Memory Leak problem when using bitmap image as tooltip?

My code:

Image img = new Image();

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water lilies.jpg");
bi.EndInit();
img.Source = bi;
btn1.ToolTip = img; //btn1 is a Button

Symptom:

Every time whenthe tooltip of "btn1" shows, some memory is consumed andwill notbe released later.

So, is it the problem of my code or the winfx?

Thanks a lot.

Rguo

By default, the BitmapCacheOption is set ot "OnDemand". This means that the image will be held onto since we need to be able to get to the image bits. Can you try the following and see if this helps:

Image img = new Image();

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water lilies.jpg");

bi.CacheOption = BitmapCacheOption.OnLoad;
bi.EndInit();
img.Source = bi;
btn1.ToolTip = img; //btn1 is a Button

Robert A. Wlodarczyk

I don't think that's a WinFX problem.I believe that, as long as the btn1 element exists, its property value (in this case, the bitmap image) is stored in memory.

So, if you really want it gone, set btn1.tooltip=null when the tooltip closes. Then that variable will get munched up by the GC. when it loses scope (i.e. when the method ends).

Blackice

By default, the BitmapCacheOption is set ot "OnDemand". This means that the image will be held onto since we need to be able to get to the image bits. Can you try the following and see if this helps:

Image img = new Image();

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water lilies.jpg");

bi.CacheOption = BitmapCacheOption.OnLoad;
bi.EndInit();
img.Source = bi;
btn1.ToolTip = img; //btn1 is a Button

Robert A. Wlodarczyk
But the MSDN says that it is BitmapCacheOption.Default which is the default behavior (Caches the entire image into memory. This is the default value.)?
d00d

You can use google to search for other answers

Custom Search

More Threads

• ScaleTransform.ScaleYProperty - XAML
• Interop and processes
• How to convert a WPF Control in to a WinForm Control?
• DataGrid - Converter for 'Content' of a CellTemplate
• KeyUp in C#, WPF
• Use Command Property in CheckBox control
• Imaging.BitmapSource lock of the source
• Need a control in WPF
• TabControl designer behavior
• How to get access to a nested business objects in another control