Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

Why does using JPEG Decode in a loop cause a memory leak?

I am using JPEG Decode to convert a JPEG stream (acquired over TCP) into an IMAQ image. This is then resized etc., before being converted into a picture for communication to a picture control via an invoke reference on another vi. (This all works fine)
My problem is that each picture received from the network and decoded in JPEG decode results in cumulative memory allocation, even though (presumably) the new image overwrites the old one. Why does this memory leak happen and how can I prevent it? Is the old image somehow stored 'beneath' the new one and does the IMAQ image therefore need purging somehow?
I am using Labview 6.0. The code is attached, but is clouded in additional code which is not relevant to
the problem so only a brave person will attempt to sift through it!
0 Kudos
Message 1 of 16
(4,402 Views)
It looks like your problem is that you are trying to build an array of buffer pointers, adding a buffer each time you receive an image. Since you keep using the same name for the buffer, all you are doing is creating a large array of buffer pointers that all point to the same image. Instead, you should just allocate the buffer once outside the loop and reuse it.

My guess on the memory leak is that the buffer array is growing slightly (just a few bytes) each loop, but if you left this running for a long time I suppose it could add up.

Bruce
Bruce Ammons
Ammons Engineering
Message 2 of 16
(4,402 Views)
Thanks for the feedback Bruce.

Yes, I am creating an array, but I mistakenly thought it was an array of pictures, not pointers. I shall modify the code to work differently and see if that solves the memory leak problem 🙂

I will still somehow need to buffer images though as I am using this technique to smooth out network delays. I guess I shall have to create several image pointers and use then sequentially.....
0 Kudos
Message 3 of 16
(4,402 Views)
That is a common mistake people make with images. Copying an image pointer doesn't copy the image inside it. Reusing the same name reuses the same memory space.

Bruce
Bruce Ammons
Ammons Engineering
0 Kudos
Message 4 of 16
(4,402 Views)
Ok, more problems....

I changed the code to create an array of 10 IMAQ image pointers. The code receives the images over the network exactly as before, and this time it sequentially takes each pointer in turn to store the JPEG Decoded image. In another loop, the images are sequentially copied to an image control in another vi.

Previously, I was mistakenly thinking that I was creating an array of images, now I realise that the array is an array of pointers. So, why do I still get a memory leak? With each jpeg stream decoded into an image I get approx. 100k of memory usage which is not regained unless I abort the vi.

Simply put, why does the use of JPEG Decode in a loop cause memory leaks? Should I be using IMAQ Copy for the new ima
ges instead of just using the pointers?

Please feel free to examine the updated code (Image_ReceiveB.vi) to see where I have gone wrong, because I have absolutely no idea....
0 Kudos
Message 5 of 16
(4,402 Views)
Are you sure it is the JPEG Decode? Try removing the part that sends it to the control, and see if the memory leak still occurs. Maybe just decode the image and display it using an IMAQ window.

If it still has a memory leak with just the JPEG Decode, post this version and we can look at it.

Bruce
Bruce Ammons
Ammons Engineering
0 Kudos
Message 6 of 16
(4,403 Views)
OK, as requested, looked at a "bare-bones" version of my code. Same thing though. I even ran it using the 'step' buttons and as soon as JPEG Decode is ran, the memory use jumps up by ~100k. Each and every time. This is definitely where the problem lies. I suspect I am not using it entirely correctly and only have myself to blame!

The new slimmer code is attached (there are now some redundant parts to it because I have removed the a large proportion that would normally access some of the information generated in the lower portion of the block diagram).
0 Kudos
Message 7 of 16
(4,403 Views)
I cleaned out your code to just create an array of 10 images, then use the JPEG decode to decompress an array of 256 image strings. I ran the loop with a 1 second delay. Each second, I saw the memory used by LV jump about 300k, which is the size of one image. After the first 10 images, the size stopped increasing and held steady as the images were replaced.

I don't think the JPEG decode is producing your memory leak. You might want to monitor the size of your string in your shift register at the top of the diagram. That is the only other item I can see that might be growing steadily.

Bruce
Bruce Ammons
Ammons Engineering
Message 8 of 16
(4,403 Views)
Thank you Bruce,

You have been incredibly helpful, I shall try what you suggest. I was convinced the problem was with the JPEG decode bit, so now I think more experimentation with the code is needed 🙂 I shall post again if I find anything interesting.

Cheers.

Richard.
0 Kudos
Message 9 of 16
(4,403 Views)
Hi Riggy,

Having gone through the discussions between you and Bruce, I gather that you are going to experiment with the code.

Please do post back your findings on the discussion forum, so that we can provide you with the appropriate feedback.

Best Regards

Atul Wahi
Appplications Engineer
www.ni.com
0 Kudos
Message 10 of 16
(4,403 Views)