使用下面这段代码后,我的内存很快就满了。Valgrind显示有内存泄漏,但是所有的分配都是在栈上进行的,在函数结束时(应该)被释放。
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显示在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)
这是OpenCV中已知的一个bug吗,还是我漏掉了什么?
CV_32SC1
是unsigned long
吗?我觉得它们更像是int
...(我想知道是否在未被允许的内存空间中编写,因为 unsigned long 在位数上比 int 更大) - B. Decoster