Ask Your Question

memory leak with VideoWriter

asked 2013-03-24 04:38:24 -0600

atrebbi gravatar image

updated 2013-03-24 05:23:08 -0600

berak gravatar image

I have a problem writing a video in iOS.

I have double checked my code, the problem is just in :


each time I call this function, i lose some memory ; it seems to me that the memory is not realeased even after _videoWriter->release() ( only a very little portion )

as i should register long videos, I wanted to try to save the video now and then, but if i close the file i had created (through method release() ) and try to reopen it , i get an error ( in general, it seems to me that if the file i'm trying to open already exists, the function fail, i can only create new video files )

this is the method that adds a frame:

            Mat frame = [MainViewController cvMatWithImage:image];


            NSLog(@"frames %d/%d", _nframeswritten, _nframes);

this is how i open the video:

    _videoWriter = new VideoWriter();
    _videoWriter->open([_videoPath UTF8String],

at the time i manage to record a video about 2 minutes lenght before it crashes on an iphone4s ( 10 fps, 288x352 )

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2014-04-13 04:21:32 -0600

atrebbi gravatar image

this bug has been fixed .

more info here:

edit flag offensive delete link more

answered 2013-03-24 16:30:37 -0600

Actually combining C - style code with C++ style code is completely wrong. So pick one and stick to it.

The code :


is C - style and requires you to release all pointers manually.

Change to the C++ interface and all pointers will be released after use for you.

edit flag offensive delete link more


I'm using c++ interface, at least I thought so ; how should I substitute _videoWriter->open() ? In any case the part of code that is giving problems is: _videoWriter->write(frame);

i have tried : *_videoWriter << frame; but i have the same problema

atrebbi gravatar imageatrebbi ( 2013-03-25 12:24:59 -0600 )edit

actually it shouldn't contain any arrows, which indicates a pointer. You should just call the functions on the object instance that you created by using the dot functionality. For example : here) or videoWriter.write(parameters here)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-25 13:44:01 -0600 )edit

Ok, just to be sure i did the changes you said, no difference at all, memory is lost in the same way

    _videoWriter = VideoWriter();[_videoPath UTF8String],


_videoWriter << frame;

( I used pointer to objects, there's nothing strange in it )

atrebbi gravatar imageatrebbi ( 2013-03-25 17:27:49 -0600 )edit

It not strange, it is just dubious to use pointers and C++ code mixed, since the C++ functionality handles the pointer releasing itself. That said, do you have the problem consisting with each possible CV_FOURCC code? Might be a decoder problem.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-26 09:25:51 -0600 )edit

Yes i have tried all the commented decoders

I think i have found the problem, trying to patch it ( bug #2918 ), there are a couple of variables that are not released : (1316)

NSLog(@"Patch alessandro trebbi");

i don't manage to compile the framework correctly and test the patch

atrebbi gravatar imageatrebbi ( 2013-03-26 18:01:22 -0600 )edit

We encountered the same memory leak on iOS. Recompiling the framework with this patch resolve the problem.

Mederic gravatar imageMederic ( 2013-12-04 03:33:18 -0600 )edit

Question Tools


Asked: 2013-03-24 04:38:24 -0600

Seen: 2,544 times

Last updated: Apr 13 '14