I am very new to OpenCV. I noticed the following code has a memory leak:
IplImage *img, *img_dest;
img = cvLoadImage("..\\..\\Sunset.jpg", CV_LOAD_IMAGE_COLOR);
while(1) // to make the mem leak obvious
{
img_dest = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3);
img_dest = cvCloneImage(img);
cvReleaseImage( &img_dest );
}
cvReleaseImage( &img );
How to release the unreferenced data then? And is there an easy way to make a clean copy of an IPL image (of course we could write a loop to copy each element of the data...).
For your memory leak issue:
cvCreateImage
allocated memoryA for the image, and cvCloneImage
allocated memoryB (and cloning whatever value stored in img
as stated in your code). cvReleaseImage(&img_dest)
only deallocate memoryB thus memoryA is left unreferenced but not deallocated.
For your IPL Image copying:
Declare another memory and use command cvCopy
, i dont see any difficulties in using it and it is safe and efficient.
If you wish to declare an IPL image header without allocating data byte for storing image value, use CreateImageHeader
instead. I would advise you to spend some time mastering cvCreateImage
, cvCreateImageHeader
, cvCreateData
, cvReleaseImage
, cvReleaseImageHeader
, cvReleaseImageData
and cvCloneImage
.