简体   繁体   English

OpenCV cv :: Mat使用std :: vector导致潜在的内存泄漏

[英]OpenCV cv::Mat causing potential memory leak with std::vector

As it stands right now im trying to save an entire list of images in the form of cv::Mats inside of a vector for later processing. 就目前而言,我试图以cv :: Mat的形式将图像的整个列表保存在向量中,以供以后处理。 Right now I have something that looks like this: 现在,我有这样的东西:

do
{
 image = readimage();
 cv::Mat mat = cv::Mat((length, width, CV_8UC4, image));
 cv::Mat temp = mat.clone();
 saved_images.push_back();

 mat.release();
 temp.release();
 freeimagememory(image);
}
while(hasimage);

This actually works. 这实际上有效。 For exceptionally small lists of images it will store them just fine. 对于非常小的图像列表,它将很好地存储它们。 However as I get to large amounts of images the program consistently crashes saying Abort() was called, and upon inspection it says it's throwing a cv::exception. 但是,当我得到大量图像时,该程序始终崩溃,并说Abort()被调用,并且在检查时它说它抛出了cv :: exception。

Does anyone know why this is? 有人知道为什么是这样吗? I've thought about changing the vector to a vector of pointers to cv::Mats in order to save space (cloning seems expensive) but I'm not sure how well that will work. 我曾考虑过将向量更改为指向cv :: Mat的指针的向量,以节省空间(克隆似乎很昂贵),但我不确定这样做的效果如何。

Can anyone help? 有人可以帮忙吗?

EDIT1: The exact error being thrown is failed to allocated X bytes. EDIT1:引发的确切错误未能分配X字节。 I assume this is because it's eating up all of the available memory somehow (even though I'm sitting on 8 gigs of memory and definitely have memory free). 我认为这是因为它以某种方式吞噬了所有可用的内存(即使我坐在8G的内存上并且肯定有可用的内存)。

EDIT2: EDIT2:

The below code also seems to work. 下面的代码似乎也有效。

std::vector<cv::Mat*> ptrvec;
do{

 image.readimage();
 ptrvec.push_back(new cv::Mat((length, width, CV_8UC4, image)));
 freeimagememory(image);
}
while(hasimage);

This one doesn't have a problem with memory (I can push all the images I want to it) but I get an access violation when I try to do 这个内存没有问题(我可以将所有想要的图像推送到其中),但是在尝试执行此操作时遇到访问冲突

cv::imshow("Test Window", *ptrvec[0]);

EDIT3: EDIT3:

Is there a chance I'm hitting the upper limit of 32 bit? 我是否有机会达到32位的上限? I'm more than capable of recompiling this into a 64 bit project. 我具有将其重新编译为64位项目的能力。

You may be running out of memory when you store 3000 color images 800 x 600 in a vector. 当您在向量中存储3000 x 800 x 600彩色图像时,可能内存不足。 Storing Mat pointers in memory will not solve your problem, since the data is still allocated in RAM. 将Mat指针存储在内存中不会解决您的问题,因为数据仍然分配在RAM中。

Check whether there is enough memory in your system to store all the images. 检查系统中是否有足够的内存来存储所有图像。 If not you can upload images in batches, for example, process the first 500 images, then process the next 500 images, etc. 如果不是,则可以批量上传图像,例如,处理前500张图像,然后处理接下来的500张图像,依此类推。

In your program you allocate the vector on the stack . 在程序中,将向量分配到堆栈上 Allocating on the heap is recommended when you need a large block of memory (your case). 当您需要大量内存(根据您的情况)时,建议在堆上分配。 So can try to allocate the vector on the heap instead (provided that you have enough memory to store the vector). 因此,可以尝试在堆上分配向量(前提是您有足够的内存来存储向量)。 See, stack vs heap , or this cpp-tutorial for more information. 有关更多信息,请参见stack vsheap此cpp-tutorial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM