opencv释放内存

发布于 2024-12-29 06:01:31 字数 522 浏览 0 评论 0原文

我已经发布了一些关于释放所有 IplImage 和所有 CvMat 结构和 CvMemStorage 的帖子,但我仍然遇到一些内存问题。

我是否还必须释放 CvPointCvScalarCvPoint* (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み青杉依旧 2025-01-05 06:01:31

这个问题与 C 相关性比 OpenCV 更多。例如,这些:

CvScalar b1;
CvScalar f;
CvPoint pt;

是局部变量,因此当它们所属的作用域 { } 完成执行时,它们会自动释放。

this:

CvPoint *point;

是一个指针,同时也是一个局部变量。您不应该deletefree()它,因为您没有通过newmalloc()为其分配任何内存。这样做会给您带来问题(可能是崩溃)。

但另一方面,data

float *data = (float*)resd->imageData;

是一个指针和保存内存块的局部变量。但是,在这种特定情况下 delete[] data;free(data) 并不明智,因为您没有直接分配此内存。很明显,该内存是作为 resd 的一部分分配的,这意味着您必须检查代码并找出变量 resd 是如何声明/初始化的,然后执行以下操作适当的程序来释放它。由于我对 OpenCV 有一点了解,我可以看出 resd 是一个 IplImage*。如果您使用 cvCreateImage() 创建此变量,那么您还需要使用 cvReleaseImage() 释放它。

最后:

CvPoint* ptsCorner=(CvPoint*) malloc(3*sizeof(ptsCorner[0]));  

这是动态内存分配的典型情况,您专门分配一定量的内存。由于 ptsCorner 是一个局部变量和一个指针,当它所属的作用域完成执行时,您将丢失对该内存块的引用,并且它将简单地丢失在您的 RAM 中,消耗内存空间并导致泄露。不用说,在这种情况下您必须执行 free() 来释放内存。

This question is much more C related than OpenCV. For instance, these:

CvScalar b1;
CvScalar f;
CvPoint pt;

are local variables, and therefore they are automatically dealocatted when the scope { } they belong finishes executing.

This:

CvPoint *point;

is a pointer, and at the same time is a local variable. You shouldn't delete or free() it because you didn't allocated any memory for it through new or malloc(). Doing so will cause you problems (probably a crash).

But data on the other hand:

float *data = (float*)resd->imageData;

is a pointer and local variable that holds a memory block. However, it's not wise to delete[] data; or free(data) in this specific case because you didn't allocate this memory directly. It's obvious that this memory was allocated as a part of resd, which means you have to check the code and find out how the variable resd was declared/initialized and then do the appropriate procedure to release it. Since I know a litle bit about OpenCV I can tell that resd is an IplImage*. If you used cvCreateImage() to create this variable then is also your job to release it with cvReleaseImage().

At last:

CvPoint* ptsCorner=(CvPoint*) malloc(3*sizeof(ptsCorner[0]));  

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 execute free() 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.

近箐 2025-01-05 06:01:31

我认为解决问题的最好方法是阅读有关指针的优秀教程。

这是一个 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文