I have a problem with catching the OutOfMemoryExce ption in a managed C+
application. When creating small objects on the managed heap neithe
the catch handler for OutOfMemoryExce ption nor the UnhandledExcept io
handler is called in an out of memory situation. Instead the applicatio
writes 'Fatal out of memory' to the console and exits
Is this a bug in the runtime or am I missing something
Here is the example code
#include "stdafx.h
#using <mscorlib.dll
using namespace System
using namespace System::Collect ions
using namespace System::Threadi ng
using namespace System::Diagnos tics
public __gc class Test
public
static void Main()
static void Test4::OnUnhand ledException( Object* pSender,
UnhandledExcept ionEventArgs* pArgs )
protected
static ArrayList* ms_pList = new ArrayList()
}
void Test4::Main(
AppDomain::Curr entDomain->UnhandledExcep tion += new UnhandledExcept ionEventHandler (
0, Test4::OnUnhand ledException )
tr
for( int n = 0 ; n < 10; ++n
tr
Console::WriteL ine("Allocating objects ...")
int i = 0
for( int m = 0; m < 100000000; ++m
ms_pList->Add(__box(i++) )
Console::WriteL ine("Clearing object list ...")
ms_pList->Clear()
Console::WriteL ine("Garbage collection ...")
GC::Collect()
catch(OutOfMemo ryException*
ms_pList->Clear()
Console::WriteL ine("OutOfMemor yException caught")
catch(...
ms_pList->Clear()
Console::WriteL ine("Unexpected exception caught")
catch(OutOfMemo ryException*
ms_pList->Clear()
Console::WriteL ine(S"OutOfMemo ryException caught in Main")
catch(...
Console::WriteL ine("Unexpected exception caught")
Console::WriteL ine(S"Applicati on finished normally")
Console::WriteL ine(S"Press <Enter> to exit")
Console::ReadLi ne()
void Test4::OnUnhand ledExceptio
Object* pSender,
UnhandledExcept ionEventArgs* pArg
ms_pList->Clear()
GC::Collect()
Console::WriteL ine(S"Unhandled exception caught:")
Console::WriteL ine(S"Press <Enter> to exit")
Console::ReadLi ne()
Process::GetCur rentProcess()->Kill()
Best regards
Michael