OpenCV Mat creation memory leak

Posted by Royi Freifeld on Stack Overflow See other posts from Stack Overflow or by Royi Freifeld
Published on 2011-11-14T09:46:42Z Indexed on 2011/11/14 9:51 UTC
Read the original article Hit count: 1494

Filed under:
|

My memory is getting full fairly quick once using the next piece of code. Valgrind shows a memory leak, but everything is allocated on stack and (supposed to be) freed once the function ends.

void mult_run_time(int rows, int cols)
{
    Mat matrix(rows,cols,CV_32SC1);
    Mat row_vec(cols,1,CV_32SC1);

    /* initialize vector and matrix */
    for (int col = 0; col < cols; ++col)
    {
        for (int row = 0; row < rows; ++row)
        {
            matrix.at<unsigned long>(row,col) = rand() % ULONG_MAX;
        }

        row_vec.at<unsigned long>(1,col) = rand() % ULONG_MAX;
    }
    /* end initialization of vector and matrix*/

    matrix*row_vec;  
}

int main()
{
    for (int row = 0; row < 20; ++row)
    {
        for (int col = 0; col < 20; ++col)
        {
            mult_run_time(row,col);
        }
    }

    return 0;
}

Valgrind shows that there is a memory leak in line Mat row_vec(cols,1,CV_32CS1):

==9201== 24,320 bytes in 380 blocks are definitely lost in loss record 50 of 50
==9201==    at 0x4026864: malloc (vg_replace_malloc.c:236)
==9201==    by 0x40C0A8B: cv::fastMalloc(unsigned int) (in /usr/local/lib/libopencv_core.so.2.3.1)
==9201==    by 0x41914E3: cv::Mat::create(int, int const*, int) (in /usr/local/lib/libopencv_core.so.2.3.1)
==9201==    by 0x8048BE4: cv::Mat::create(int, int, int) (mat.hpp:368)
==9201==    by 0x8048B2A: cv::Mat::Mat(int, int, int) (mat.hpp:68)
==9201==    by 0x80488B0: mult_run_time(int, int) (mat_by_vec_mult.cpp:26)
==9201==    by 0x80489F5: main (mat_by_vec_mult.cpp:59)

Is it a known bug in OpenCV or am I missing something?

© Stack Overflow or respective owner

Related posts about c++

Related posts about opencv