G
Guest
I have a problem with catching the OutOfMemoryException in a managed C+
application. When creating small objects on the managed heap neithe
the catch handler for OutOfMemoryException nor the UnhandledExceptio
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::Collections
using namespace System::Threading
using namespace System:iagnostics
public __gc class Test
public
static void Main()
static void Test4::OnUnhandledException( Object* pSender,
UnhandledExceptionEventArgs* pArgs )
protected
static ArrayList* ms_pList = new ArrayList()
}
void Test4::Main(
AppDomain::CurrentDomain->UnhandledException += new UnhandledExceptionEventHandler(
0, Test4::OnUnhandledException )
tr
for( int n = 0 ; n < 10; ++n
tr
Console::WriteLine("Allocating objects ...")
int i = 0
for( int m = 0; m < 100000000; ++m
ms_pList->Add(__box(i++))
Console::WriteLine("Clearing object list ...")
ms_pList->Clear()
Console::WriteLine("Garbage collection ...")
GC::Collect()
catch(OutOfMemoryException*
ms_pList->Clear()
Console::WriteLine("OutOfMemoryException caught")
catch(...
ms_pList->Clear()
Console::WriteLine("Unexpected exception caught")
catch(OutOfMemoryException*
ms_pList->Clear()
Console::WriteLine(S"OutOfMemoryException caught in Main")
catch(...
Console::WriteLine("Unexpected exception caught")
Console::WriteLine(S"Application finished normally")
Console::WriteLine(S"Press <Enter> to exit")
Console::ReadLine()
void Test4::OnUnhandledExceptio
Object* pSender,
UnhandledExceptionEventArgs* pArg
ms_pList->Clear()
GC::Collect()
Console::WriteLine(S"Unhandled exception caught:")
Console::WriteLine(S"Press <Enter> to exit")
Console::ReadLine()
Process::GetCurrentProcess()->Kill()
Best regards
Michael
application. When creating small objects on the managed heap neithe
the catch handler for OutOfMemoryException nor the UnhandledExceptio
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::Collections
using namespace System::Threading
using namespace System:iagnostics
public __gc class Test
public
static void Main()
static void Test4::OnUnhandledException( Object* pSender,
UnhandledExceptionEventArgs* pArgs )
protected
static ArrayList* ms_pList = new ArrayList()
}
void Test4::Main(
AppDomain::CurrentDomain->UnhandledException += new UnhandledExceptionEventHandler(
0, Test4::OnUnhandledException )
tr
for( int n = 0 ; n < 10; ++n
tr
Console::WriteLine("Allocating objects ...")
int i = 0
for( int m = 0; m < 100000000; ++m
ms_pList->Add(__box(i++))
Console::WriteLine("Clearing object list ...")
ms_pList->Clear()
Console::WriteLine("Garbage collection ...")
GC::Collect()
catch(OutOfMemoryException*
ms_pList->Clear()
Console::WriteLine("OutOfMemoryException caught")
catch(...
ms_pList->Clear()
Console::WriteLine("Unexpected exception caught")
catch(OutOfMemoryException*
ms_pList->Clear()
Console::WriteLine(S"OutOfMemoryException caught in Main")
catch(...
Console::WriteLine("Unexpected exception caught")
Console::WriteLine(S"Application finished normally")
Console::WriteLine(S"Press <Enter> to exit")
Console::ReadLine()
void Test4::OnUnhandledExceptio
Object* pSender,
UnhandledExceptionEventArgs* pArg
ms_pList->Clear()
GC::Collect()
Console::WriteLine(S"Unhandled exception caught:")
Console::WriteLine(S"Press <Enter> to exit")
Console::ReadLine()
Process::GetCurrentProcess()->Kill()
Best regards
Michael