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

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Simon Shakeshaft

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
Reply | Threaded
Open this post in threaded view
|

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

Christopher Targett-Adams
Hi Simon, 

I've come across numerous similar issues myself when wrapping my own c++ classes in .net. I've learned to be suspicious of any static user defined type that transitions between native and managed, be it via a singleton class or not.

My solution to your problem as I think you touched upon is to use a static private member pointer to the sole instance of the singleton rather than a static variable within a GetInstance() method:
ie

private:
 X *_instance;
public:
 X* GetInstance();

Rather than the
X GetInstance()
{
 X &x;
 return x;
}
way.

Does mean you may have worry about clean up but I usually don't worry too much about that.

Cheers,
Chris

Sent from my iPhone

On 6 Aug 2012, at 09:42, "Simon Shakeshaft" <[hidden email]> wrote:

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

------------------------------------------------------------------------------
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
Reply | Threaded
Open this post in threaded view
|

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

Simon Shakeshaft

Hi Chris,

Thanks for confirming that.

Cheers

Simon

 

From: Chris T-Adams [mailto:[hidden email]]
Sent: 06 August 2012 18:45
To: Simon Shakeshaft
Cc: <[hidden email]>
Subject: Re: [Quantlib-users] Issues with C++/CLI wrappers and local static variables in quantlib

 

Hi Simon, 

 

I've come across numerous similar issues myself when wrapping my own c++ classes in .net. I've learned to be suspicious of any static user defined type that transitions between native and managed, be it via a singleton class or not.

 

My solution to your problem as I think you touched upon is to use a static private member pointer to the sole instance of the singleton rather than a static variable within a GetInstance() method:

ie

 

private:

 X *_instance;

public:

 X* GetInstance();

 

Rather than the

X GetInstance()

{

 X &x;

 return x;

}

way.

 

Does mean you may have worry about clean up but I usually don't worry too much about that.

 

Cheers,

Chris


Sent from my iPhone


On 6 Aug 2012, at 09:42, "Simon Shakeshaft" <[hidden email]> wrote:

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


------------------------------------------------------------------------------
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
Reply | Threaded
Open this post in threaded view
|

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

Luigi Ballabio
In reply to this post by Simon Shakeshaft
Hi Simon,
    we moved the instance to the local scope because we had problems
with other compilers, but this might not be relevant anymore.  If you
post a patch, I'll be happy to try it.

Luigi

On Mon, Aug 6, 2012 at 10:42 AM, Simon Shakeshaft
<[hidden email]> wrote:

> 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
>

------------------------------------------------------------------------------
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
Reply | Threaded
Open this post in threaded view
|

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

Simon Shakeshaft
Hi Luigi,

In the end I had to make changes to singleton.hpp and all the currency
classes in america.hpp, africa.hpp, asia.hpp, europe.hpp and oceania.hpp.
The currency classes also required the addition of a null deleter on the
boost::shared_ptr declaration to  overcome the CLR shutdown issues.

It may be good just to park the code in a new folder along with a readme,
rather than merge/test the changes into the existing codebase?

Simon.

-----Original Message-----
From: Luigi Ballabio [mailto:[hidden email]]
Sent: 24 August 2012 10:28
To: Simon Shakeshaft
Cc: [hidden email]
Subject: Re: [Quantlib-users] Issues with C++/CLI wrappers and local static
variables in quantlib

Hi Simon,
    we moved the instance to the local scope because we had problems with
other compilers, but this might not be relevant anymore.  If you post a
patch, I'll be happy to try it.

Luigi

On Mon, Aug 6, 2012 at 10:42 AM, Simon Shakeshaft
<[hidden email]> wrote:

> 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/sta
> tic-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
>

------------------------------------------------------------------------------
The Windows 8 Center - In partnership with Sourceforge
Your idea - your app - 30 days.
Get started!
http://windows8center.sourceforge.net/
what-html-developers-need-to-know-about-coding-windows-8-metro-style-apps/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users

CLI Wrapper Patches.7z (8K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

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

Luigi Ballabio
Hi Simon,
    yes, we might possibly park them somewhere for review.  If you're
familiar with git, you could fork the quantlib repository at
<https://github.com/lballabio/quantlib> and commit your changes there.
 Let me know if you do that.

Luigi

On Mon, Oct 29, 2012 at 10:43 AM, Simon Shakeshaft
<[hidden email]> wrote:

> Hi Luigi,
>
> In the end I had to make changes to singleton.hpp and all the currency
> classes in america.hpp, africa.hpp, asia.hpp, europe.hpp and oceania.hpp.
> The currency classes also required the addition of a null deleter on the
> boost::shared_ptr declaration to  overcome the CLR shutdown issues.
>
> It may be good just to park the code in a new folder along with a readme,
> rather than merge/test the changes into the existing codebase?
>
> Simon.
>
> -----Original Message-----
> From: Luigi Ballabio [mailto:[hidden email]]
> Sent: 24 August 2012 10:28
> To: Simon Shakeshaft
> Cc: [hidden email]
> Subject: Re: [Quantlib-users] Issues with C++/CLI wrappers and local static
> variables in quantlib
>
> Hi Simon,
>     we moved the instance to the local scope because we had problems with
> other compilers, but this might not be relevant anymore.  If you post a
> patch, I'll be happy to try it.
>
> Luigi
>
> On Mon, Aug 6, 2012 at 10:42 AM, Simon Shakeshaft
> <[hidden email]> wrote:
>> 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/sta
>> tic-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
>>

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

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

Simon Shakeshaft
Hi Luigi,

Finally got round to forking the repository and parking the 'CLI safe'
versions of singleton.hpp and the various currency classes.  I have added a
comment to the existing singleton.hpp which clarifies the use of 'Construct
on first use Idiom' and the 'static initialisation order fiasco', which is
why the code is as is.

Also to the fork I have added 2 test cases for the Thirty360 daycounter
(BondBasis and EurobondBasis) if they are useful.

And  a small correction to the flag usage in runtest.bat.

[3 separate commits].

Simon.



-----Original Message-----
From: Luigi Ballabio [mailto:[hidden email]]
Sent: 31 October 2012 15:45
To: Simon Shakeshaft
Cc: [hidden email]
Subject: Re: [Quantlib-users] Issues with C++/CLI wrappers and local static
variables in quantlib

Hi Simon,
    yes, we might possibly park them somewhere for review.  If you're
familiar with git, you could fork the quantlib repository at
<https://github.com/lballabio/quantlib> and commit your changes there.
 Let me know if you do that.

Luigi

On Mon, Oct 29, 2012 at 10:43 AM, Simon Shakeshaft
<[hidden email]> wrote:

> Hi Luigi,
>
> In the end I had to make changes to singleton.hpp and all the currency
> classes in america.hpp, africa.hpp, asia.hpp, europe.hpp and oceania.hpp.
> The currency classes also required the addition of a null deleter on
> the boost::shared_ptr declaration to  overcome the CLR shutdown issues.
>
> It may be good just to park the code in a new folder along with a
> readme, rather than merge/test the changes into the existing codebase?
>
> Simon.
>
> -----Original Message-----
> From: Luigi Ballabio [mailto:[hidden email]]
> Sent: 24 August 2012 10:28
> To: Simon Shakeshaft
> Cc: [hidden email]
> Subject: Re: [Quantlib-users] Issues with C++/CLI wrappers and local
> static variables in quantlib
>
> Hi Simon,
>     we moved the instance to the local scope because we had problems
> with other compilers, but this might not be relevant anymore.  If you
> post a patch, I'll be happy to try it.
>
> Luigi
>
> On Mon, Aug 6, 2012 at 10:42 AM, Simon Shakeshaft
> <[hidden email]> wrote:
>> 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/st
>> a
>> tic-variable-in-native-method-causes-exception-c0020001-during-proces
>> s
>> -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
>>



------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

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

Luigi Ballabio
Thanks, I'll try and have a look.

Luigi


On Sat, Nov 17, 2012 at 5:59 PM, Simon Shakeshaft
<[hidden email]> wrote:

> Hi Luigi,
>
> Finally got round to forking the repository and parking the 'CLI safe'
> versions of singleton.hpp and the various currency classes.  I have added a
> comment to the existing singleton.hpp which clarifies the use of 'Construct
> on first use Idiom' and the 'static initialisation order fiasco', which is
> why the code is as is.
>
> Also to the fork I have added 2 test cases for the Thirty360 daycounter
> (BondBasis and EurobondBasis) if they are useful.
>
> And  a small correction to the flag usage in runtest.bat.
>
> [3 separate commits].
>
> Simon.
>
>
>
> -----Original Message-----
> From: Luigi Ballabio [mailto:[hidden email]]
> Sent: 31 October 2012 15:45
> To: Simon Shakeshaft
> Cc: [hidden email]
> Subject: Re: [Quantlib-users] Issues with C++/CLI wrappers and local static
> variables in quantlib
>
> Hi Simon,
>     yes, we might possibly park them somewhere for review.  If you're
> familiar with git, you could fork the quantlib repository at
> <https://github.com/lballabio/quantlib> and commit your changes there.
>  Let me know if you do that.
>
> Luigi
>
> On Mon, Oct 29, 2012 at 10:43 AM, Simon Shakeshaft
> <[hidden email]> wrote:
>> Hi Luigi,
>>
>> In the end I had to make changes to singleton.hpp and all the currency
>> classes in america.hpp, africa.hpp, asia.hpp, europe.hpp and oceania.hpp.
>> The currency classes also required the addition of a null deleter on
>> the boost::shared_ptr declaration to  overcome the CLR shutdown issues.
>>
>> It may be good just to park the code in a new folder along with a
>> readme, rather than merge/test the changes into the existing codebase?
>>
>> Simon.
>>
>> -----Original Message-----
>> From: Luigi Ballabio [mailto:[hidden email]]
>> Sent: 24 August 2012 10:28
>> To: Simon Shakeshaft
>> Cc: [hidden email]
>> Subject: Re: [Quantlib-users] Issues with C++/CLI wrappers and local
>> static variables in quantlib
>>
>> Hi Simon,
>>     we moved the instance to the local scope because we had problems
>> with other compilers, but this might not be relevant anymore.  If you
>> post a patch, I'll be happy to try it.
>>
>> Luigi
>>
>> On Mon, Aug 6, 2012 at 10:42 AM, Simon Shakeshaft
>> <[hidden email]> wrote:
>>> 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/st
>>> a
>>> tic-variable-in-native-method-causes-exception-c0020001-during-proces
>>> s
>>> -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
>>>
>
>

------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users