Ask Your Question
0

Opencv Resize memory leak

asked Jul 2 '13

carlosb gravatar image

updated Jul 9 '13

I am using the cv::resize function from opencv (2.4.3). I am getting a memory leak. This it the code:

   Mat src(height_,width_,CV_8UC3,(void *)&bits_[0]);
   cv::resize(src, src,cv::Size(width,height));
   BMPImage result (src);
   src.release();
   src = NULL;

I have a memory leak in cv::resize... This code is used a lot of times (It resizes hundred images in an execution) and the memory doesn't stop to increase.... I was checking the opencv documentation but It explains that cv::resize handles its memory...

In my log, it get the error from the deallocation:

0x22081 call 0x6118 <?deallocate@?$AutoBuffer@E$0BAAI@@cv@@QAEXXZ>

Update

I fixed the memory leak problem.... with this code

Mat src(height_,width_,CV_8UC3,(void *)&bits_[0]);
Mat dst;
cv::resize(src, dst,cv::Size(width,height),0,0,INTER_CUBIC);
BMPImage result (dst);
src.release();
dst.release();
src = NULL;
dst = NULL;

This code doesn't fix the deallocation problem with it is related to:

src.release();
src = NULL;

If I have a moment, I will investigate it.

Preview: (hide)

Comments

again : src.release() does nothing, and dst.release() will be called automatically on end of scope.

but where do your bits_ come from originally ? if they were dynamically allocated, you'll have to delete[] them manually after use.

berak gravatar imageberak (Jul 9 '13)edit
1

berak, you are right, but the problem was in the bits_ array.... Also, I removed src.release() and dst.release(). Thanks!

carlosb gravatar imagecarlosb (Jul 9 '13)edit

2 answers

Sort by » oldest newest most voted
2

answered Jul 9 '13

Niu ZhiHeng gravatar image

I think if "bits_" is allocated somewhere else, you need to explicitly deallocate it after use.

Preview: (hide)
3

answered Jul 2 '13

berak gravatar image

oh, note, that Mat's created with a 'borrowed' pointer don't release the 'borrowed' memory.

i guess, the err is due to 'recycling' that src mat for the resize operation. so src.release() still thinks, there's nothing to do

please try to use a fresh Mat for the resize:

Mat src(height_,width_,CV_8UC3,(void *)&bits_[0]);
Mat dst;
cv::resize(src, dst, cv::Size(width,height));
BMPImage result (dst);
//src.release(); // no more nessecary, not even for dst
//src = NULL;
Preview: (hide)

Question Tools

Stats

Asked: Jul 2 '13

Seen: 6,623 times

Last updated: Jul 09 '13