您的位置:首页 > 运维架构

__BEGIN__; __END__ - [OPENCV]

2013-03-11 11:19 204 查看
文章原始出处和作者信息及本声明

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

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

#define__BEGIN__{
#define__END__gotoexit;exit:;}


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

voidcvResizeDCT(CvMat*input_array,CvMat*output_array)

{

CvMat*temp_array=0;//declarepointerthatshouldbereleasedanyway.

CV_FUNCNAME("cvResizeDCT");//declarecvFuncName


__BEGIN__;//startprocessing.Theremaybesomedeclarationsjustafter

//thismacro,buttheycouldnotbeaccessedfromtheepilogue.


if(!CV_IS_MAT(input_array)||!CV_IS_MAT(output_array))

//useCV_ERROR()toraiseanerror

CV_ERROR(CV_StsBadArg,

"input_arrayoroutput_arrayarenotvalidmatrices");

//somerestrictionsthataregoingtoberemovedlater,maybechecked

//withCV_ASSERT()

CV_ASSERT(input_array->rows==1&&output_array->rows==1);

//useCV_CALLforsafefunctioncall

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__;//finishprocessing.Epiloguefollowsafterthemacro.

//releasetemp_array.Iftemp_arrayhasnotbeenallocated

//beforeanerroroccured,cvReleaseMat

//takescareofitanddoesnothinginthiscase.

cvReleaseMat(&temp_array);

}


intmain(intargc,char**argv)

{

CvMat*src=cvCreateMat(1,512,CV_32F);

#if1/*noerrors*/

CvMat*dst=cvCreateMat(1,256,CV_32F);

#else

CvMat*dst=0;/*testerrorprocessingmechanism*/

#endif

cvSet(src,cvRealScalar(1.),0);

#if0/*change0to1tosuppresserrorhandlerinvocation*/

cvSetErrMode(CV_ErrModeSilent);

#endif

cvResizeDCT(src,dst);//ifsomeerroroccurs,themessage

//boxwillpopup,oramessagewillbe

//writtentolog,orsomeuser-defined

//processingwillbedone

if(cvGetErrStatus()<0)

printf("Someerroroccured");

else

printf("EverythingisOK");

return0;

}


http://shijuanfeng.blogbus.com/logs/196580440.html这是我之前做练习的一个例子,在最后一个改进的程序中,可以看到,写了好几遍

if(!flag)

{

fclose(fp);

return0;

}


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

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

明天年后,希望自己可以抽个奖,嘎嘎~~~今天自觉加班了快一小时,总结今天一天所学,走喽~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: