Monday, January 19, 2009 

Visual C++ 2008 bug (C++/CLI)

I wrote some faulty code recently. Actually, the code was right, but the compiler didn't like it anyway. It took a while to discover it was a (known) compiler bug, so I hope I can save someone else's time by providing a link to relevant the Microsoft page: Static variable in native method causes exception c0020001 during process exit.

Shortly stated, if you use a local static variable (inside a function/method), and the type of that variable has a native/unmanaged destructor, the compiler gets confused and emits a managed destructor instead. Of course, that happens if your compilation unit is in mixed mode (native + managed).

Unfortunately, in my case the local static variable was inside a template, shared between native and mixed compilation units. In the end, we didn't adopt Microsoft suggestion of a separate unit (which kinda makes all the C++/CLI magic to disappear), but I just changed the type of the static to remove the destructor. That requires some tweaking, and I still hope we'll get a fix from Microsoft, although the page above reports a discouraging "Won't fix" decision...

Labels: , ,