opencv释放内存
我已经发布了一些关于释放所有 IplImage
和所有 CvMat
结构和 CvMemStorage
的帖子,但我仍然遇到一些内存问题。
我是否还必须释放 CvPoint
、CvScalar
、CvPoint*
(3 个 CvPoint 的数组,我是否也必须释放每个元素?)
如果我必须释放所有这些东西,我该怎么办?我没有找到任何函数可以这样做。我在 C/C++ 中使用 OpenCV 2.1。
我是这样声明它们的:
CvScalar b1;
CvScalar f;
float *data=(float*)resd->imageData; (need to release data)
CvPoint *point;
CvPoint pt;
CvPoint* ptsCorner=(CvPoint*) malloc(3*sizeof(ptsCorner[0]));
I already some post about releasing all the IplImage
and all the CvMat
structure and CvMemStorage
, but still I have some memory problems.
Do I have to release also CvPoint
, CvScalar
, CvPoint*
(array of 3 CvPoints, do i have to free each element too?)
If I have to release all this stuff, how do I? I did not find any function to do so. I'm using OpenCV 2.1 in C/C++.
Here is how I declare them:
CvScalar b1;
CvScalar f;
float *data=(float*)resd->imageData; (need to release data)
CvPoint *point;
CvPoint pt;
CvPoint* ptsCorner=(CvPoint*) malloc(3*sizeof(ptsCorner[0]));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题与 C 相关性比 OpenCV 更多。例如,这些:
是局部变量,因此当它们所属的作用域
{ }
完成执行时,它们会自动释放。this:
是一个指针,同时也是一个局部变量。您不应该
delete
或free()
它,因为您没有通过new
或malloc()为其分配任何内存
。这样做会给您带来问题(可能是崩溃)。但另一方面,
data
:是一个指针和保存内存块的局部变量。但是,在这种特定情况下
delete[] data;
或free(data)
并不明智,因为您没有直接分配此内存。很明显,该内存是作为 resd 的一部分分配的,这意味着您必须检查代码并找出变量 resd 是如何声明/初始化的,然后执行以下操作适当的程序来释放它。由于我对 OpenCV 有一点了解,我可以看出resd
是一个IplImage*
。如果您使用cvCreateImage()
创建此变量,那么您还需要使用cvReleaseImage()
释放它。最后:
这是动态内存分配的典型情况,您专门分配一定量的内存。由于 ptsCorner 是一个局部变量和一个指针,当它所属的作用域完成执行时,您将丢失对该内存块的引用,并且它将简单地丢失在您的 RAM 中,消耗内存空间并导致泄露。不用说,在这种情况下您必须执行 free() 来释放内存。
This question is much more C related than OpenCV. For instance, these:
are local variables, and therefore they are automatically dealocatted when the scope
{ }
they belong finishes executing.This:
is a pointer, and at the same time is a local variable. You shouldn't
delete
orfree()
it because you didn't allocated any memory for it throughnew
ormalloc()
. Doing so will cause you problems (probably a crash).But
data
on the other hand:is a pointer and local variable that holds a memory block. However, it's not wise to
delete[] data;
orfree(data)
in this specific case because you didn't allocate this memory directly. It's obvious that this memory was allocated as a part ofresd
, which means you have to check the code and find out how the variableresd
was declared/initialized and then do the appropriate procedure to release it. Since I know a litle bit about OpenCV I can tell thatresd
is anIplImage*
. If you usedcvCreateImage()
to create this variable then is also your job to release it withcvReleaseImage()
.At last:
It's the tipical case of dynamic memory allocation, where you specifically allocate an amount of memory. Since
ptsCorner
is a local variable and a pointer, when the scope it belongs to finishes executing you will loose the reference to that memory block and it will simply be lost in your RAM, caonsuming memory space and causing a leak. It's needless to say that you must executefree()
to deallocate the memory in this case.如果您使用 new 初始化 CVPoint 结构,那么您将需要调用 delete (如果是数组,则调用 delete[] )以避免内存泄漏。
如果没有,那么当函数超出范围时,变量将被自动销毁。
如果您发布代码,那么会更容易看到。
If you are initializing the CVPoint structure using new then yes you will need to call delete (or delete[] if it's an array) to avoid leaking memory.
If not then when the function goes out of scope the variables will be automatically destroyed.
If you post your code then it will be easier to see.
我认为解决问题的最好方法是阅读有关指针的优秀教程。
这是一个 http://www.cplusplus.com/doc/tutorial/pointers/
你所说的OpenCV存在问题,实际上是对编程语言缺乏理解。所以,从基础开始,继续!
I think the best way to solve your problems is to read a good tutorial about pointers.
Here is one http://www.cplusplus.com/doc/tutorial/pointers/
What you say there are OpenCV problems, actually is a lack of understanding of programming languages. So, start with the basics, ad go on!