_ Begin __; _ end _ is an error handling mechanism in opencv, which can prevent illegal memory release and Memory leakage.

Source: Internet
Author: User

Http://shijuanfeng.blogbus.com/logs/205062662.html

_ Begin __; _ end _ is an error handling mechanism in opencv, which can prevent illegal memory release and Memory leakage.

#define __BEGIN__ {#define __END__ goto exit; exit: ; }

 

An example of how to prevent the memory from being illegally released.

void cvResizeDCT( CvMat* input_array, CvMat* output_array )
{
    CvMat* temp_array = 0; // declare pointer that should be released anyway.
    CV_FUNCNAME( "cvResizeDCT" ); // declare cvFuncName
 
    __BEGIN__; // start processing. There may be some declarations just after
    // this macro, but they could not be accessed from the epilogue.
 
    if( !CV_IS_MAT(input_array) || !CV_IS_MAT(output_array) )
        // use CV_ERROR() to raise an error
        CV_ERROR( CV_StsBadArg,
        "input_array or output_array are not valid matrices" );
    // some restrictions that are going to be removed later, may be checked
    // with CV_ASSERT()
    CV_ASSERT( input_array->rows == 1 && output_array->rows == 1 );
    // use CV_CALL for safe function call
    CV_CALL( temp_array = cvCreateMat( input_array->rows,
        MAX(input_array->cols,
        output_array->cols),
        input_array->type ));
    if( output_array->cols > input_array->cols )
        CV_CALL( cvZero( temp_array ));
    temp_array->cols = input_array->cols;
    CV_CALL( cvDCT( input_array, temp_array, CV_DXT_FORWARD ));
    temp_array->cols = output_array->cols;
    CV_CALL( cvDCT( temp_array, output_array, CV_DXT_INVERSE ));
    CV_CALL( cvScale( output_array,
        output_array,
        1./sqrt((double)input_array->cols*output_array->cols), 0 ));
 
    __END__; // finish processing. Epilogue follows after the macro. 
    // release temp_array. If temp_array has not been allocated
    // before an error occured, cvReleaseMat
    // takes care of it and does nothing in this case.
    cvReleaseMat( &temp_array );
}
 
int main( int argc, char** argv )
{
    CvMat* src = cvCreateMat( 1, 512, CV_32F );
#if 1 /* no errors */
    CvMat* dst = cvCreateMat( 1, 256, CV_32F );
#else
    CvMat* dst = 0; /* test error processing mechanism */
#endif
    cvSet( src, cvRealScalar(1.), 0 );
#if 0 /* change 0 to 1 to suppress error handler invocation */
    cvSetErrMode( CV_ErrModeSilent );
#endif
    cvResizeDCT( src, dst ); // if some error occurs, the message
    // box will popup, or a message will be
    // written to log, or some user-defined
    // processing will be done
    if( cvGetErrStatus() < 0 )
        printf("Some error occured" );
    else
        printf("Everything is OK" );
    return 0;
}

 

Http://shijuanfeng.blogbus.com/logs/196580440.html this is an example of my previous practice. In the last improved program, I can see that I wrote it several times.

if ( !flag )        
{            
       fclose(fp);          
       return 0;       
 }

Use _ begin __; _ end _ to avoid writing like this all the time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.