That doesn’t meet the requirements of the specification, which says:
Inevitably, that type has C linkage; the entire header containing its definition is bracketed with
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
Now, on platforms where the “system” C and C++ compilers are basically the same compiler (and come from the same source as the linker), you’ll probably get away with differences in linkage (so long as you’re not trying to export the function from one compilation unit and import it into another, because functions with C++ linkage have their names mangled to support overloading).
But in any case, it doesn’t matter that you’re throwing from a C++ function, but that you’re throwing through a C function. Somewhere on the call stack between the callback and the try
/catch
will be at least one C function (the OpenGL API function that invoked the callback). When an exception is thrown, the exception-handling code generated by the compiler has to unwind the call stack until it finds a handler. And a C++ compiler isn’t required to know how to do this for C functions, and a C compiler certainly isn’t required to know how to deal with exceptions. Because C functions can only (legally) call functions with C linkage (whether directly or through function pointers), and C++ functions with C linkage aren’t allowed to throw exceptions (because code generated by a C compiler doesn’t know how to deal with them).
It probably works if the C compiler, C++ compiler and linker are all part of “Microsoft Developer Studio”, and they’re the same versions which were used to compile both opengl32.dll and the ICD. It probably works if the C and C++ compilers are both gcc
, the linker is GNU ld
, and they’re the same versions used to build all of the myriad fragments that Linux’ OpenGL implementation has been split into of late.
But in general, anyone following your suggestion of throwing exceptions from the debug callback is opening the door to the kind of problems that aren’t exactly simple to diagnose even by someone familiar with the low-level details of compilation and linking, and all but impossible for those who aren’t.
If it works, it works. If it doesn’t, you get a crash, quite possibly with a backtrace that’s complete gibberish for good measure.