I have a Visual Studio 2008 Windows Mobile 6.5.3 ARMV4I project. I check for out of memory conditions using a try/catch block looking for std::bad_alloc exceptions. But, through testing I’ve found that it can actually just return a NULL value and not throw an exception.
int _tmain( int argc, _TCHAR* argv[] )
{
int i = 0;
try
{
for( ; i < 30000; ++i )
{
BYTE* f = new BYTE[1024];
if( NULL == f )
{
NKDbgPrintfW( L"NULL - Survived %d iterations\r\n", i );
break;
}
}
}
catch( std::bad_alloc& )
{
NKDbgPrintfW( L"std::bad_alloc - Survived %d iterations\r\n", i );
}
return 0;
}
This prints: NULL - Survived 29599 iterations.
I am not linking against nothrownew.obj and according to this I should expect a std::bad_alloc exception . http://msdn.microsoft.com/en-us/library/kftdy56f%28v=VS.90%29.aspx
Does anybody know what’s going on?
Thanks,
PaulH
What you observe simply means that the library supplied with the compiler is broken. It is broken in a sense that it does not follow the requirements of C++ language standard.
This particular issue with
newhas been present in the earlier versions of C++ standard library (like the one supplied with VC 6.0). Later some versions of the compiler/library were updated to satisfy standard requirements. Apparently, the Windows Mobile version was left unchanged.It is quite possible that it was done intentionally in order to preserve the compatibility with older code. You might also wanna check for some compiler configuration switch that might control this behavior. I don’t know whether such a switch exists.