本文主要是介绍__BEGIN__; __END__是opencv中的一种错误处理机制,它可以防止非法的内存释放,以及内存泄露,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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__就可以避免一直这样子写
这篇关于__BEGIN__; __END__是opencv中的一种错误处理机制,它可以防止非法的内存释放,以及内存泄露的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!