andrea
andrea

Reputation: 1358

release memory opencv

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]));   

Upvotes: 4

Views: 4290

Answers (3)

karlphillip
karlphillip

Reputation: 93468

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.

Upvotes: 5

Sam
Sam

Reputation: 20056

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!

Upvotes: 0

TomP89
TomP89

Reputation: 1460

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.

Upvotes: 1

Related Questions