Ask Your Question
0

Opencv Resize memory leak

asked 2013-07-02 10:10:19 -0600

carlosb gravatar image

updated 2013-07-09 03:43:01 -0600

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.

edit retag flag offensive close merge delete

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 ( 2013-07-09 04:06:02 -0600 )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 ( 2013-07-09 07:09:56 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2013-07-09 06:51:44 -0600

Niu ZhiHeng gravatar image

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

edit flag offensive delete link more
3

answered 2013-07-02 12:05:00 -0600

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;
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-07-02 10:10:19 -0600

Seen: 6,061 times

Last updated: Jul 09 '13