怕不经意间改动了什么东西,特地新建一个静态库,写了个函数产生异常:int test()
{
try
{
MessageBox(GetFocus(),"开始","test",MB_OK);
LPRECT lprc = (LPRECT)0xCDCDCDCD;
return lprc->left;
}
catch(...)
{
MessageBox(GetFocus(),"异常已捕获","test",MB_OK);
return 0;
}
}程序连接DEBUG版本的静态库,调用test运行正常,能弹出"异常已捕获"的信息,但是连接Release版本的静态库,调用test则无法捕获异常,系统报错内存0xCDCDCDCD不能读。奇怪的是我在调用程序test的时候加try,catch又能捕获异常,即:
try
{
test();
}
catch(...)
{
MessageBox(GetFocus(),"程序捕获异常","test",MB_OK);
}
是能捕获异常的,但这是程序捕获的,而不是在静态库里捕获。
不管程序是Debug还是Release版本,只要连接Release版本的静态库,test函数内部的异常捕获无效,照此情况,那我岂不是无法写出安全的静态库了?大家碰到过这种情况吗?
{
try
{
MessageBox(GetFocus(),"开始","test",MB_OK);
LPRECT lprc = (LPRECT)0xCDCDCDCD;
return lprc->left;
}
catch(...)
{
MessageBox(GetFocus(),"异常已捕获","test",MB_OK);
return 0;
}
}程序连接DEBUG版本的静态库,调用test运行正常,能弹出"异常已捕获"的信息,但是连接Release版本的静态库,调用test则无法捕获异常,系统报错内存0xCDCDCDCD不能读。奇怪的是我在调用程序test的时候加try,catch又能捕获异常,即:
try
{
test();
}
catch(...)
{
MessageBox(GetFocus(),"程序捕获异常","test",MB_OK);
}
是能捕获异常的,但这是程序捕获的,而不是在静态库里捕获。
不管程序是Debug还是Release版本,只要连接Release版本的静态库,test函数内部的异常捕获无效,照此情况,那我岂不是无法写出安全的静态库了?大家碰到过这种情况吗?
{
test();
}
catch(...)
{
MessageBox(GetFocus(),"程序捕获异常","test",MB_OK);
}
这段代码在Release版本的程序中是有效的,能捕获异常,但是在Release的静态库里无法捕获。
----------------
你怎么解释我Release版本程序中能catch到异常?
不过你可以把test()的函数签名改成 test() throw()试一下
总之我觉得这都是优化的结果
搜索下怎么把编译选项改更异步模式,改后很耗CPU和内存等资源就是
-----------------
是啊,现在问题就是为什么放到静态库就不起作用
LPRECT lprc = (LPRECT)NULL;
试试。
#include "stdio.h"int test1();void main()
{
try
{
printf("%d",test1());
}
catch(...)
{
printf("catched in main function\n");
}
}int test1()
{
try
{
LPRECT lprc = (LPRECT)0xCDCDCDCD;
return lprc->left;
}
catch(...)
{
printf("catched\n");
return 0;
}
}运行结果:
DEBUG版本:
catched
0RELEASE版本:
catched in main functionRELEASE版本在函数中try,catch无效,而只在main函数中有效?
下面是从MSDN上的 “Exception Handling: Default Synchronous Exception Model”摘抄的:
In previous versions of Visual C++, the C++ exception handling mechanism supported asynchronous (hardware) exceptions by default. Under the asynchronous model, the compiler assumes any instruction may generate an exception.With the new synchronous exception model, now the default, exceptions can be thrown only with a throw statement. Therefore, the compiler can assume that exceptions happen only at a throw statement or at a function call. This model allows the compiler to eliminate the mechanics of tracking the lifetime of certain unwindable objects, and to significantly reduce the code size, if the objects' lifetimes do not overlap a function call or a throw statement. The two exception handling models, synchronous and asynchronous, are fully compatible and can be mixed in the same application.Catching hardware exceptions is still possible with the synchronous model. However, some of the unwindable objects in the function where the exception occurs may not get unwound, if the compiler judges their lifetime tracking mechanics to be unnecessary for the synchronous model.See the /GX and /EH compiler options for information on enabling exception handling.