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]));
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
deleteorfree()it because you didn’t allocated any memory for it throughnewormalloc(). Doing so will cause you problems (probably a crash).But
dataon 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 variableresdwas declared/initialized and then do the appropriate procedure to release it. Since I know a litle bit about OpenCV I can tell thatresdis 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
ptsCorneris 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.