Free opencv memory

I already have a message about the release of all IplImage and the entire structure of CvMat and CvMemStorage , but still I have memory problems.

Should I also release CvPoint , CvScalar , CvPoint* (an array of 3 CvPoints, do I need to also free every element?)

If I need to release it all, how am I? I did not find any function for this. I am using OpenCV 2.1 in C / C ++.

Here's 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])); 
+4
source share
3 answers

This question is much more related to C than OpenCV. For example, they:

 CvScalar b1; CvScalar f; CvPoint pt; 

are local variables, and therefore they are automatically deactivated when the { } region belonging to them finishes execution.

It:

 CvPoint *point; 

is a pointer and at the same time is a local variable. You should not delete or free() , because you did not allocate any memory for it through new or malloc() . This will cause a problem (possibly a malfunction).

But data on the other hand:

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

is a pointer and a local variable that contains a block of memory. However, this is unreasonable for delete[] data; or free(data) in this particular case, because you did not allocate this memory directly. Obviously, this memory was allocated as part of resd , which means that you need to check the code and find out how the resd variable was declared / initialized, and then follow the appropriate procedure to release it. Since I know the litle bit about OpenCV, I can say that resd is IplImage* . If you used cvCreateImage() to create this variable, then it is also your task to free it with cvReleaseImage() .

Finally:

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

This is a typical case of dynamic memory allocation, where you specifically allocate memory. Since ptsCorner is a local variable and a pointer, when the region to which it belongs finishes executing, you will lose a reference to this memory block and it will simply be lost in your RAM, free up memory space and cause a leak. Needless to say, you must execute free() to free memory in this case.

+5
source

If you initialize the CVPoint structure with new , then yes, you will need to call delete (or delete [] if it is an array) to avoid memory leak.

If not, then when the function goes out of scope, the variables will be automatically destroyed.

If you post your code, it will be easier to see.

+1
source

I think the best way to solve your problems is to read a good tutorial on pointers.

Here is one http://www.cplusplus.com/doc/tutorial/pointers/

What are you saying, there are OpenCV problems, in fact, this is a lack of understanding of programming languages. So, start with the basics, the ad will continue!

0
source

Source: https://habr.com/ru/post/1392880/


All Articles