From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using the C# dll to update the .NET PictureBox causes memory leak

Hi,

I use a C# dll that acquired images from a camera and updates a .NET PictureBox in a VI. The .NET PictureBox reference is provided to the dll at first call. Everything runs smoothly, except that there is a memory leak which is cause by updating the .NET PictureBox. Below is the code in C# that .NET PictureBox and disposes the old image.

Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
/* bitmap manipulation code */

// Assign a temporary variable to dispose the bitmap after assigning the new bitmap to the display control.
Bitmap bitmapOld = pictureBoxe.Image as Bitmap;

// Provide the display control with the new bitmap. This action automatically updates the display.
pictureBoxe.Image = bitmap;
if (bitmapOld != null)
{
	// Dispose the bitmap.
	bitmapOld.Dispose();
}

I would appreciate any suggestion on how to solve the issue.

0 Kudos
Message 1 of 5
(4,359 Views)

If you post the LabVIEW code, we can help you with that. Good chance the leak is there. 

 

For the c# code, a c# forum might be more appropriate. You'll find more c# experts there.

 

I'd say bitmap needs to closed, since you do a new on it, but I'm no c# expert.

0 Kudos
Message 2 of 5
(4,328 Views)

Labview (LV) code is just one dll call where the .NET PictureBox reference is passed to the dll. Everything else is done in the dll. On the LV side VI just waits that the dll stops executing. Old Bitmap is properly disposed on each update.

I tested following scenarios:

- Updating the .NET PictureBox in LV directly (the dll was not used) - no leak.

- Disabling the update of the .NET PictureBox call in the dll - no leak.

- Updating the .NET PictureBox control in C# with the same dll - no leak

 

Therefore, it must be some issue because of the LV and C# interaction.

0 Kudos
Message 3 of 5
(4,322 Views)

You really need to show more than that. The Bitmap has indeed a Dispose() method but that is not enough as far as LabVIEW is concerned. The LabVIEW .Net refnum is also a resource that needs to be closed. And while for many >Net objects it's enough to close the refnum in LabVIEW to dispose both the LabVIEW refnum and the underlying .Net object it is for some strange reasons indeed necessary to first call the Dispose() method before closing the LabVIEW refnum in order to make sure that all gets disposed.

 

It is totally unclear how you pass in the Bitmap refnum to the DLL but pictureBoxe seems to be a cluster or something. So show as the whole code (sans analysis if that is top secret) and the according VI and LabVIEW project so we can make an informed recommendation as to what you might further try.

Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 5
(4,308 Views)

Attached is the code in LV that creates camera object, passes .NET PictureBox control reference to the dll, starts camera grab and waits until user stops it. Nothing else is done in LV. All data (image) manipulation (create image, display in the LV control, dispose image) is done in C#.

Here are the steps that are executed:

1. In LV - Create cameras object.

2. In LV - Send .NET PictureBox control reference to the dll.

3. In LV - Start camera.

4. In C# - Wait until camera acquires a next image.

5. In C# - Process image and create a Bitmap (which is child class of the Image class).

6. In C# - Get current Image from the LV .NET PictureBox control (as bitmapOld).

7. In C# - Updated the LV .NET PictureBox control with a new image.

8. In C# - Dispose the old Image (bitmapOld).

9. In LV - Wait until stop button is pressed.

10. In LV - Stop camera.

11. In LV - Destroy camera.

 

I hope below code makes it more clear.

 

private PictureBox pictureBoxe = null; //PictureBox is a .NET control


// Set PictureBoxes.
public void SetPictureBoxes(PictureBox[] pictureBoxes)
{
	this.pictureBoxe = pictureBoxes[0];
}


// Occurs when an image has been acquired and is ready to be processed.
private void OnImageGrabbed(Object sender, ImageGrabbedEventArgs e)
{
	/* grab image from camera */
	
	Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
	// Bitmap is a child class of Image class
	
	/* bitmap manipulation code */

	// Assign a temporary variable to dispose the bitmap (Image) after assigning the new bitmap to the display control.
	Bitmap bitmapOld = pictureBoxe.Image as Bitmap; // Get Image from LV PictureBox

	// Provide the display control with the new bitmap (Image). This action automatically updates the display.
	pictureBoxe.Image = bitmap; // Updates the LV PictureBox
	if (bitmapOld != null)
	{
		// Dispose the bitmap (Image).
		bitmapOld.Dispose(); // Dispose Image that was previously shown on the LV pictureBox
	}
}

 

0 Kudos
Message 5 of 5
(4,283 Views)