c# picturebox memory releasing problem

picciopiccio picture picciopiccio · Dec 2, 2009 · Viewed 20k times · Source

I'm a newby in C#. I have to repeatedly refresh a GUI picture box in a worker thread. The image is acquired from a camera polling a driver with a GetImage method that retrives the image to be displayed. Even if I allocate the bitmap using directive "using" and explicitly call G.C, memory seems to be never deallocated.

The worker thread is something like this:

   while (true)
    {
        // request image with IR signal values (array of UInt16)
        image = axLVCam.GetImage(0);
        lut = axLVCam.GetLUT(1);
        DrawPicture(image, lut);
        //GC.Collect();

    }

While the DrawPicture method is something like

   public void DrawPicture(object image, object lut)
{

  [...]

    // We have an image - cast it to proper type
    System.UInt16[,] im = image as System.UInt16[,];
    float[] lutTempConversion = lut as float[];

    int lngWidthIrImage = im.GetLength(0);
    int lngHeightIrImage = im.GetLength(1);

    using (Bitmap bmp = new Bitmap(lngWidthIrImage, lngHeightIrImage)) {

      [...many operation on bitmap pixel...]

        // Bitmap is ready - update image control

        //SetControlPropertyThreadSafe(tempTxtBox, "Text", string.Format("{0:0.#}", lutTempConversion[im[160, 100]]));

        //tempTxtBox.Text = string.Format("{0:00000}", im[160, 100]);
        //System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
        pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
    }
}

Problems arises with the

pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());

In fact commenting that line of code, garbage collection works as it would. Better, the problem seems to be with

System.Drawing.Image.FromHbitmap(bmp.GetHbitmap())

Any advice to solve this memory leak?

Thanks a lot!

Answer

Fredrik Mörk picture Fredrik Mörk · Dec 2, 2009

Image implements IDisposable, so you should call Dispose on each Image instance that you create, when it is no longer needed. You could try to replace this line in your code:

pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());

With this:

if (pic.Image != null)
{
    pic.Image.Dispose();
}
pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());

This will dispose the previous image (if any) before the new one is assigned.