Hi,
I have a similar issue to this old thread - http://old.nabble.com/Settings%3A%3Ainstance%28%29.evaluationDate%28%29-produces-exception-in-managed-code-to25479768.html#a25479768 I wrap quantlib.lib to a managed C++ dll and referenced it from an ASP.NET aspx web page in C#. It looks fine until it calls Settings::instance().evaluationDate(). I have a different exception but I think the cause is the same - "The function must be called from the default domain" It fails in allocation a static variable in singleton.hpp static std::map<Integer, boost::shared_ptr<T> > instances_; template <class T> #if defined(QL_PATCH_MSVC) && defined(_MANAGED) inline // this seems to be required when CLR support is enabled #endif inline T& Singleton<T>::instance() { static std::map<Integer, boost::shared_ptr<T> > instances_; I did some search on the web, found some same issues but did not find any solutions. e.g. http://social.msdn.microsoft.com/Forums/en-US/clr/thread/cd3c34a6-84f9-4e2b-a483-12e179eb84b7 And there quite a few posts on the web about problems using static variable in unmanaged codes in a managed application. Anyone has success in implementing for quantlib as described? Or any suggestions are very apprecilated. Also, is SWIG the answer? I've never used it. Have tried to load QuantLib_vc9.sln from ..\QuantLib-SWIG-1.0\CSharp to 2008 C# built fine but get some errors when running the examples. Any documents explain how to run quantlib SWIG? Thanks |
The source of the problem is that there are AppDomains in the .Net world. An
AppDomain is a light-weight process. Every thread in the CLR lives in an AppDomain. See http://blogs.msdn.com/cbrumme/archive/2003/06/01/51466.aspx for more details.
Native C++ static variables does not know anything about AppDomains. Therefore they should not be initialize in the code of managed compiled cpp files. You can get by with this if your code is in the primary AppDomain. This is not the case if you are writing an ASP.Net solution. In ASP.Net, the Internet Information Server (IIS) is the primary AppDomain. I came across the same problem went I wrote unit tests for my application. Nunit.exe has the primary AppDomain and placed my tests in a secondary AppDomain. The workaround I used is to set each QuantLib static variable in a global function that uses "#pragma managed". See http://social.msdn.microsoft.com/Forums/en-US/clr/thread/cd3c34a6-84f9-4e2b-a483-12e179eb84b7 Here is an example. #pragma managed(push, off) void SetQuantLibEvaluationDate(QuantLib::Date date) { QuantLib::Settings::instance().evaluationDate() = date; } #pragma managed(pop) Since you are using ASP.Net, you should defined QL_ENABLE_SESSIONS and write a Integer sessionID() function that returns an unique integer for each user. Otherwise, all of your users will shared the same settings. On Sat, Mar 6, 2010 at 7:02 PM, Cliffy <[hidden email]> wrote:
------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Great, Nathan
Thanks for your help. By the way do you think if there is any issues if I wrap #pragma managed(push, off) and #pragma managed(pop) around the definition of singleton template (i.e. in singleton.hpp) since I am not sure if there are any other implicit calls to Singleton::instance. I have tried it and it seems to be working but I am not sure if this could cause other issues. oh, great point for QL_ENABLE_SESSIONS as well , Regards, Cliffy
|
By the way do you think if there is any issues if I
wrap #pragma managed(push, off) and #pragma managed(pop) around the definition of singleton template (i.e. in singleton.hpp That is a good question. I have to think about. If you do it, I think that you should also do it for all the classes that inherits from Singleton. In fact, I think you should do it for all class that are defined only in header files especially the classes that also has any static variables. An example of this is the currencies classes. I had to write wrapper functions for them also. for example #pragma managed(push, off) QuantLib::Currency CreateUSDCurrency() { return QuantLib::USDCurrency(); } #pragma managed(pop) #pragma managed(push, off) QuantLib::Currency CreateEURCurrency() { return QuantLib::EURCurrency(); } #pragma managed(pop) ------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Free forum by Nabble | Edit this page |