cvShowImage() with Cocoa causes serious memory leak on Mac 10.6 x86_64 (Bug #253)


Added by Ryan Lei almost 15 years ago. Updated over 12 years ago.


Status:Done Start date:
Priority:High Due date:
Assignee:Vadim Pisarevsky % Done:

0%

Category:highgui-gui
Target version:-
Affected version: Operating System:
Difficulty: HW Platform:
Pull request:

Description

Today I built r2968 by CMake with commands

sudo cmake -G "Unix Makefiles" -D BUILD_NEW_PYTHON_SUPPORT=OFF -D BUILD_TESTS=OFF -D WITH_CARBON=OFF -D WITH_QUICKTIME=OFF .
sudo make -j8
sudo make install

which gave QTKit and Cocoa support.

Then with the following simple webcam capturing code:

int main() {
    [[IplImage]] *frame;
    [[IplImage]] *resized;
    /* Example 2-9: Input from a camera */
    cvNamedWindow("USB Camera");
    [[CvCapture]] *camera = cvCaptureFromCAM( 0 );
    assert( camera != NULL );

    while ( true ) {
        frame = cvQueryFrame( camera );
        resized = cvCreateImage(cvSize(800, 600), IPL_DEPTH_8U, 3);
        cvResize(frame, resized);
        cvShowImage("USB Camera", resized);
    if ( cvWaitKey( 10 ) == 32 ) break;

    cvReleaseImage(&resized);
    }
    cvReleaseCapture( &camera );
    return 0;
}

caused serious memory leak as in this youtube video.
The variable resize does not matter with the memory leak, and cvReleaseImage(&frame) gives OpenCV exceptions as indicated in the doc.

The same source code runs WITHOUT memory leak with the library previously built with make_framework.sh and r2492, giving QuickTime and Carbon support.
I think the problem lies in the file src/highgui/*cvcap_qt.mm*.


window_cocoa.patch - Fix for memory leak, etc. (17.8 kB) Nicholas Butko, 2010-06-14 12:55 am


Associated revisions

Revision 4ec4cf68
Added by Vadim Pisarevsky over 14 years ago

fixed memory leaks in cocoa bindings (trac ticket #253). Thanks to N. Butko

Revision 19229f2f
Added by Vadim Pisarevsky about 12 years ago

Merge pull request #253 from Nerei:smart_operators_for_smart_ptr

History

Updated by Ryan Lei almost 15 years ago

btw, I think the key line is

frame = cvQueryFrame( camera );

, which returns a pointer to an entire Iplimage.

Updated by anonymous - almost 15 years ago

Replying to [comment:1 ryanleitaiwan]:

btw, I think the key line is

> frame = cvQueryFrame( camera );

, which returns a pointer to an entire Iplimage.

I have made similar tests, it seems that the memory leak is in the cvShowImage()

Updated by Giorgio B. almost 15 years ago

I've made a test too. The memory leak is very likely to be caused by cvShowImage()

I'm working on Mac OS X 10.6.3 + OpenCV r3146 with Cocoa and QTKit.

Is anybody working on that? This problem is very frustrating! :(

Regards,
Giorgio

Replying to [comment:3 anonymous]:

Replying to [comment:1 ryanleitaiwan]:

btw, I think the key line is

> > frame = cvQueryFrame( camera );

, which returns a pointer to an entire Iplimage.

I have made similar tests, it seems that the memory leak is in the cvShowImage()

Updated by Nicholas Butko almost 15 years ago

I am changing the summary to reflect the content of the thread (the problem is in cvShowImage())

Updated by anonymous - almost 15 years ago

FYI, same problem in new Python bindings; /samples/python/camera.py hogs over a GB in a matter of seconds. Commenting out the call to cv.ShowImage makes the problem go away.

This is on on x86_64 OSX10.6 of course. Note that I used a regular build line of just:
cmake -G "Unix Makefiles"
...that is, no specific build options. Seems to be core.

Updated by Nicholas Butko over 14 years ago

I attached a patch that should fix the memory leak. There are also other miscellaneous fixes.

There is still some cleaning up that could be done on window_cocoa, but it should be lower priority. Please continue to report bugs if you see them.

Updated by anonymous - over 14 years ago

I just applied the patch to latest SVN, seems to run nicely. The memory leak has disappeared with the fix. Thank you!

Updated by Ryan Lei over 14 years ago

Cheers! It's finally fixed. Is this the status going to be changed to closed?

Updated by Ryan Lei over 14 years ago

Cheers! It's finally fixed. Is the status going to be changed to closed?

Updated by Nicholas Butko over 14 years ago

  • (deleted custom field) set to fixed

Updated by Brian Gerkey over 14 years ago

  • Status changed from Done to Cancelled
  • (deleted custom field) deleted (fixed)

Updated by Brian Gerkey over 14 years ago

  • Status deleted (Cancelled)

Updated by Vadim Pisarevsky over 14 years ago

Thanks! The patch was applied in r3253

  • Status set to Done
  • (deleted custom field) set to fixed

Updated by Ricardo Zilleruelo over 14 years ago

I still observe the memory leak.
The code below just display an image over and over again. This has a memory leak, around 1MB per second.
I compile the code linking to opencv svn trunk code. Rev: 3914.
g
+ opencv_camera2.cpp -o opencv_camera -L/Users/zeta/opencv/opencv/lib/ -I/Users/zeta/opencv/opencv/include/opencv/ -lhighgui

#include "highgui.h"
int main(){
IplImage frame;
CvCapture
capture = cvCaptureFromCAM(CV_CAP_ANY);
cvNamedWindow("x",CV_WINDOW_AUTOSIZE);
frame = cvQueryFrame(capture);
while(1) cvShowImage("x",frame);
return 0;
}

  • Status changed from Done to Cancelled
  • (deleted custom field) deleted (fixed)

Updated by Nicholas Butko over 14 years ago

I tested the code, and there is no memory leak.

You are probably linking to the wrong library. The correct library is "opencv_highgui" and not "highgui".

I am using r3825, but I checked out r3914, and there are no differences that should cause a leak to reappear. The differences are almost negligible and only affect windows platforms.

Here was my compile script:
g++ opencv_camera2.cpp -o opencv_camera -L/usr/local/lib -lopencv_highgui -I/usr/local/include/opencv

  • Status changed from Cancelled to Done
  • (deleted custom field) set to worksforme

Updated by Andrey Kamaev over 12 years ago

  • Category changed from highgui-images to highgui-gui

Also available in: Atom PDF