Exception allocation on MinGW looses ::GetLastError() value

Timothy Madden terminatorul@gmail.com
Wed Mar 28 18:57:00 GMT 2012


Hello

I just wrote a simple MinGW program (more or less, the library templates 
there are not necessarily small) where I built an exception class around 
the "System Error Codes" in Win API returned by ::GetLastError() function.

Than I want to throw such an exception, with the call to GetLastError 
directly as the argument to the constructor:

	throw WinapiException(::GetLastError())

Much to my surprise, ::GetLastError() appears to return 0 (success), 
even after an API function returns failure. I used an API monitoring 
tool (listing all calls to .dll functions from my program), and I find 
that upon the first throw statement encountered, libgcc_s_dw2-1.dll also 
calls ::CreateSemaphore() and ::TlsAlloc(), and even ::GetLastError(), 
before my WinapiException() constructor gets called. I used gdb to break 
on ::CreateSemaphore, to confirm the backtrace. I guess it is something 
like an "exception allocation code" generated by the compiler, with 
library support.

Which is ok, except that it looses the value for ::GetLastError().

I don't want to be blunt, but it kind of breaks the API function, and 
regular users do not know to look for and use an API monitoring tool. 
Can this be fixed please ?

MinGW is now on gcc v4.6.2 (issue also reported on 4.6.1), and the folks 
there say they do not modify gcc sources. The issue was first posted there:
     http://thread.gmane.org/gmane.comp.gnu.mingw.user/38821

You can see my code here (file winapi++.hh, line 168, has the throw 
statement):

api-generics.hh http://pastebin.com/xPZU6JZd
winapi++.hh	http://pastebin.com/YU1Cuqir
winapi++.cc	http://pastebin.com/JqTEgPnY
realpath.cc	http://pastebin.com/pvTHfQUJ
Makefile	http://pastebin.com/UjZj89Kx

Thank you,
Timothy Madden





More information about the Gcc-help mailing list