set_terminate doesn't work (VS2005 beta 2)

I have a program which is generating an exception somewhere and terminating, but I don't know where. In order to find our, I added a call to set_terminate(), but it doesn't work. To test it, I am doing this:

set_terminate(TerminateHandler);
throw "Test";

I understand that set_terminate does not work if the process is being debugged (which is very impractical and frustrating, BTW), so I am testing without the debugger present. When I run my program, I get a message box like this at the point I call throw:

Microsoft Visual C++ Runtime Library

Runtime Error!

Program:

This application has requested the Runtime to terminate in an unusual way. Please contact the application's support team for more information.

What am I doing wrong? I have read the MSDN docs over and over, but cannot understand why this is not working. I have also tried calling signal(SIGABRT), in order to trap the abort, but that doesn't work, either.

[1001 byte] By [hyslopc] at [2007-12-16]
# 1

I tried a small sample and it looks like it is working as expected. The terminate handler function is actually called.

Try compiling the below code (cl /EHsc a.cpp) and then run a.exe:

#include<exception>
#include<iostream>

using namespace std;

void termfunction( )
{
cout << "In termfunction" << endl;
}

int main( )
{
terminate_handler oldHand = set_terminate(termfunction);

// Throwing an unhandled exception would also terminate the program
throw "Test";

// The program could also be explicitely terminated with:
// terminate( );
}

The output will be something like:

In termfunction
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

From the output, it indicates that termfunction was called since "In termfunction" was printed.

Am I missing something in your question?

Thanks,
Ayman Shoukry
VC++

AymanShoukry at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2
Same here. Please double check how you use set_terminate and look on call stack to debug issue.

Thanks,
Nikola
VC++

NikolaDudar at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 3
Thanks guys - you're right - your test program works fine. It's not so trivial to debug this problem since you can't reproduce it in the debugger, but I started the program up and then while the VC++ messagebox was on-screen attached to the process from VS 2005. That showed me the problem: the terminate function is called in a 3rd party DLL we are linking with. Is it so that the terminate function is process-specific, and not module-specific? And is it so that it's last-in best-dressed? If so then I guess that would explain what I'm seeing.
hyslopc at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 4
In multithreaded case, you have one terminate per thread. In case of EXE and loaded DLL, a DLL can redifine terminate function set in EXE or another DLL.

Thanks,
Nikola
VC++

NikolaDudar at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...