Search

UnhandledExceptionEventHandler is not called when managed code is called from unmanaged and throws an exception by Dimonchik1

Closed
as Fixed Help for as Fixed

1
0
Sign in
to vote
Type: Bug
ID: 454731
Opened: 5/20/2009 9:38:36 AM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
I have two problems:
1) My application is using ATL. So when I just include ATL headers and build it with default settings it just crashes during initialization stage.

2) If I call managed code from standard C++ main() function - it never fires UnhandledExceptionEventHandler in case of any exception.

I suppose the problem arises because I'm calling managed code from unmanaged, and Default Application Domain may initialize incorrectly ? Is there is a simpler way to initialize ATL ?!
Details (expand)
Product Language
English

Version

Visual Studio 2008 SP1
Operating System
Windows XP Professional
Operating System Language
English
Steps to Reproduce
1) Create "C++ Console CLR Application" using VC2008 wizard
2) Replace main.cpp with the code below:

// test_unh.cpp : main project file.

#include "stdafx.h"

#include <atlbase.h>
#include <atlcom.h>

using namespace System;
using namespace System::Windows::Forms;

static void UnhandledHandler(Object^ sender, UnhandledExceptionEventArgs^ args)
{
MessageBox::Show("Unhandled exception!");
}

void managed_main()
{
AppDomain::CurrentDomain->UnhandledException += gcnew UnhandledExceptionEventHandler(&UnhandledHandler);

ATL::CComPtr<IUnknown> pUnk;
pUnk.CoCreateInstance(L"MyComponent");

Console::WriteLine(L"Hello World");

throw gcnew Exception("test exception");
}

#pragma unmanaged

void main()
{
managed_main();
}

3) Build as managed with linker option "/ENTRY" set clear - so that native main() function is called first.
I use this trick to initialize global variables in ATL and CRT since we are using these libs. Otherwise if application is built with default project settings (/ENTRY:managed_main) - it crashes deep in CLR initialization code because of ATL includes. Stack trace:

>    test_unh.exe!_initterm(void** pfbegin = 0x00408178, void pfend = ) Line 130    C++
    test_unh.exe!<CrtImplementationDetails>::LanguageSupport::InitializeNative() Line 555    C++
    test_unh.exe!<CrtImplementationDetails>::LanguageSupport::_Initialize() Line 678    C++
    test_unh.exe!<CrtImplementationDetails>::LanguageSupport::Initialize() Line 876    C++
    test_unh.exe!?.cctor@@$$FYMXXZ() Line 922 + 0x9 bytes    C++


4) Run the App
Actual Results
The App crashes because managed "test exception" is generated, but UnhandledHandler() is never called although it's set.
Expected Results
UnhandledHandler() must be called in any case exception is thrown, be it a native or managed thread.
TAP Code (if applicable)
 
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey. [Details]

 

Sign in to post a comment.
Posted by Microsoft on 7/31/2009 at 8:14 PM
Thank you for taking the time to report this problem. The behavior is indeed a bug caused by the CLR execution engine and the CRT competing for the UnhandledExceptionFilter. The architecture of the CLR has been revised in the 4.0 version supporting this scenario.

For Visual Studio 2008 you can customize the setup of the CRT if you do not need std::terminate functionality by adding

extern "C" int __cdecl __CxxSetUnhandledExceptionFilter(){ return 0; }

to your source base.

Holger Grund
Visual C++ Libraries
Posted by Microsoft on 5/22/2009 at 12:45 AM
Thanks for the feedback. We are escalating this bug to the product unit who works on that specific feature area. The team will review this issue and make a decision on whether they will fix it or not for the next release.

Thank you,
Visual Studio Product Team
Sign in to post a workaround.