Issues with C++/CLI wrappers and local static variables in quantlib

Posted by Simon Shakeshaft on
URL: http://quantlib.414.s1.nabble.com/Issues-with-C-CLI-wrappers-and-local-static-variables-in-quantlib-tp189.html

Hi,


I've been looking at building some C++/CLI wrappers around the library, partially as an exercise to become more familiar with CLI programming and partially to compare the approach with SWIG. 

 

However, I've run into a issue which may be a compiler issue but Microsoft have it closed as 'Won't Fix'. The issue manifested it first in the Singleton [patterns] class, as I was trying to migrate the FRA example across to C#, using some of the wrapped classes I already had. In the Singleton class the instance() method declares a static variable of a native type with a destructor.

 

However the compiler registers a managed destructor with atexit and when the assembly is unloaded the CLR is already shut down, and an attempt to transition back to managed code results in an exception which cannot be caught.
e.g.
First-chance exception at 0x76a2b9bc in app.exe: 0xC0020001: The string binding is invalid.
Unhandled exception at 0x76a2b9bc in app.exe: 0xC0020001: The string binding is invalid.

 

The case on Microsoft Connect is here:

https://connect.microsoft.com/VisualStudio/feedback/details/336844/static-variable-in-native-method-causes-exception-c0020001-during-process-exit

 

The exception generates the annoying '... .exe has encountered a problem and needs to close' dialog.  This can be worked around by the use of the SetErrorMode(), prior to exit, however the exception is just masked at this point and the exit is not clean.

 


It's possible to move the local static variable in the Singleton class to the class level as a private member, and I've done this on my working copy - that works fine, the exit is now clean.  However similar code exists in many of the QuantLib classes - the specific currency classes are a case in point.

 


I'd be quite happy to work through the classes and pass the changes across, if this would be useful, otherwise I can work around by using SetErrorMode() etc.
  
Best Regards
Simon 
 
 


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users