Yochai Timmer
Yochai Timmer

Reputation: 49251

Programmatically catching windows close event when out of GDI resources

I'm trying to catch all unexpected terminations in my process.

I've used these functions to catch all the unhandled errors and exceptions that i can think of, but still it's not enough:

    SetUnhandledExceptionFilter(OnUnhandledNativeException);

    set_terminate(set_terminateHandler);

    set_unexpected(set_unexpectedHandler);

    _set_purecall_handler(set_purecallHandler);

    _set_new_handler(HandleProgramMemoryDepletion);       

    _set_invalid_parameter_handler(InvalidParameterHandler);

    signal(SIGABRT, sigabrt_handler); 
    signal(SIGINT, sigabrt_handler);
    signal(SIGTERM , sigabrt_handler);

These functions catch almost any error in the application.

But, when the application is terminated because of a GDI failure (for example GDI out of resources), non of these functions are called.

Does anyone know how i can catch GDI error events ?
I'm sure there must be some way to overload it and change the callback function.

And, does anyone know of any other weird scenarios where these functions just aren't enough ?

Note:

The exact error that it's not catching is "A required resource was unavailable".
This is caused when you create a lot of GDI objects and don't release them.
The program will crash because there aren't enough resources long before it runs out of memory.

Upvotes: 0

Views: 608

Answers (3)

Yochai Timmer
Yochai Timmer

Reputation: 49251

Ok, solved the problem.

It WAS catching the crash.

The problem was that as part of the process of salvaging the data from the crash, it was opening a Form which was supposed to notify the user that an error has occurred.

Of course, we're out of GDI objects, so the Form can't be drawn, so that in itself threw and exception, we had another unhandled exception, and the process really crashed.

Upvotes: 0

Yakov Galka
Yakov Galka

Reputation: 72479

  1. Some of the functions you listed (e.g. SetUnhandledExceptionFilter) set the handlers for the current thread. Therefore you should call them in each thread.
  2. You can add Vectored Exception Handling to the list (AddVectoredExceptionHandler).

Upvotes: 1

SmacL
SmacL

Reputation: 22922

If you're on Visual C++ __try __finally will usually do the trick. Note that this is not a portable solution.

Upvotes: 0

Related Questions