__BEGIN__; __END__ - [OPENCV]

来源:互联网 发布:淘宝卖游戏账号 编辑:程序博客网 时间:2024/06/05 03:36

文章原始出处和作者信息及本声明
http://shijuanfeng.blogbus.com/logs/205062662.html


__BEGIN__; __END__是opencv中的一种错误处理机制,它可以防止非法的内存释放,以及内存泄露

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

 

防止内存非法释放的一个例子,从opencv文档中粘出来的

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 这是我之前做练习的一个例子,在最后一个改进的程序中,可以看到,写了好几遍

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

这句代码,用__BEGIN__; __END__就可以避免一直这样子写

不过貌似直接写个宏更简单呢,呵呵,反正也算是种方法~~~

 

明天年后,希望自己可以抽个奖,嘎嘎~~~今天自觉加班了快一小时,总结今天一天所学,走喽~~~