1

我在使用 OpenCV 释放内存时遇到问题。如何释放返回指针的内存?

这是我的代码:

这是我调用函数细化的函数:

int preprocessing_and_classification() { 

/// Section 2D - Perform morphological processing 

            CvMat* image1_edge_morpho = thinning(image1_edge); 
                        ....... 
                        ....... 
                        ........... 

                        //////////////// release the memory /////////////// 
            cvReleaseImage(&img); 
            cvReleaseMat(&image1); 
            cvReleaseMat(&image1_edge); 
            cvReleaseMat(&image1_edge_morpho); 
            cvReleaseImage(&imgg); 
            cvReleaseImage(&imggT); 
            cvReleaseMemStorage(&g_storage); 
            cvReleaseMat(&features); 
            cvReleaseMat(&labels_obtained); 
            ///////////////////////////////////////////////////// 
return num_bacilli; 

} 

您可以在下面看到的函数变薄返回指针 Merode:

CvMat* thinning(CvMat* M){ 

/* 
Input parameters 
CvMat* M = Pointer to the matrix on which morphological processing needs to be done. 

Output parameters 
CvMat* Merode = Pointer to morphological processed matrix. 
*/ 



    CvMat* Merode = cvCreateMat(h,w,CV_8UC1); 
    . 
    . 
    . 
    . 
    . 
    . 



    ///// Releasing the images, matrices and structuring elements to free memroy. 
   // Matrix Merode is not released as it is the output of this function file 
    cvReleaseStructuringElement(&B); 
    cvReleaseMat( &Mopen ); 
    cvReleaseMat( &temp ); 
    cvReleaseMat( &Mclose ); 
    cvReleaseMat( &temp1 ); 
    cvReleaseMat( &MM ); 
    cvReleaseImage( &img1 ); 
    cvReleaseMemStorage(&g_storage); 
    cvReleaseMat( &Mimg1 ); 
    cvReleaseMat( &Mimg2 ); 


    return Merode; 
} 

如何删除 Merode?

4

1 回答 1

0

如果您被允许更改thinning您可以使用的函数,cv::Mat或者cv::Ptr<cvMat>(或者甚至std::unieue_ptrstd::shared_ptr使用自定义删除器 - 在 c++11 兼容编译器的情况下)

如果不允许更改代码,则应在使用该方法的任何位置删除返回指针。最简单的方法是用cv::Ptr<>.

于 2012-07-19T07:03:49.003 回答