iosobjective-copencviplimage

iOS - OpenCV Releasing IplImage crash


I have an problem freeing up memory allocated for an IplImage. Just use cvReleaseImage

cvReleaseImage(&dilateImage);

Code crashes at the line where I free "dilateImage".

Any suggestions please?

Thanks:)

- (void)processImage:(cv::Mat&)image;
{
   cv::Mat grayImage;
   cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY);

   Canny(grayImage, grayImage, 66.0, 133.0, 3);
   cv::adaptiveThreshold( grayImage, grayImage, 255,cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 11, 2 );

   IplImage img = IplImage(grayImage);
   IplImage* dilateImage = cvCloneImage(&img);

   IplConvKernel* kernal = cvCreateStructuringElementEx(3,1, 1, 0, CV_SHAPE_RECT);
   cvDilate(dilateImage, dilateImage, kernal, 2);
   cvErode(dilateImage, dilateImage, kernal, 4);
   cvDilate(dilateImage, dilateImage, kernal, 2);
   kernal = cvCreateStructuringElementEx(1, 3, 0, 1, CV_SHAPE_RECT);
   cvErode(dilateImage, dilateImage, kernal, 1);
   cvDilate(dilateImage, dilateImage, kernal, 2);

   image = cv::cvarrToMat(dilateImage);
   cvReleaseImage(&dilateImage);//<~Crash
}

Solution

  • You are passing in the image as a reference (cv::Mat&), so it is possible that something outside of this method is still holding on to the image.

    Could you try passing it in by value and seeing if the crash still occurs?