Login  Register

Re: pure virtual function call in observable update

Posted by imachabeli on Aug 12, 2011; 6:15pm
URL: http://quantlib.414.s1.nabble.com/pure-virtual-function-call-in-observable-update-tp13453p13455.html

Alan,
Even if you solve all problems with loading and running, your setup with mixed mode dll linking to static "quantlib +you wrapper" smells like a problem. When dll is unloaded in the ASP you might experience segmentation  fault due to a bug in the .net framework that ms does not plan to fix ("Static variable in native method causes exception" http://connect.microsoft.com/VisualStudio/feedback/details/336844/static-variable-in-native-method-causes-exception-c0020001-during-process-exit)

Just today I finished testing the project that pretty much does the same as what you try to achieve. I introduced extra native dll that links in quantlib and than mixed mode dll uses native one.
In the header that is included in the mixed dll source code there is no reference to anything from quantlib.

Native dll has interface like this
class QLWRAPPER_API ITermStructureHandleNative
        {
                protected:
                        boost::shared_ptr<TermStructureNative> ts_;
                       
                        ITermStructureHandleNative(boost::shared_ptr<TermStructureNative> ts ):  ts_(ts) {}

                public:
                        friend class ITermStructureNativeFactory;
                        double spread();
                        void setSpread(double s);

                        QLWrapDate maxDate();
                        int settlementDays() ;
                        QLWrapDate settlementDate()  ;
                       
                        ICalendarHandleNative* calendar() ;
                        double forwardRate(double t1, double t2, int c );
                        double forwardRateAct360(QLWrapDate d1, QLWrapDate d2, int c);
                        double forwardRate30360(QLWrapDate d1, QLWrapDate d2, int c);
                        double discount(QLWrapDate d1);
        };


TermStructureNative is defined in the separate header in the native dll as
        //this is just a dummy wrapper to avoid inclusion of qunatlib in ITermStructurNativeFactory.h
        class TermStructureNative
        {
                public:
                boost::shared_ptr<SimpleQuote> spreadQuote_;
                boost::shared_ptr<ForwardSpreadedTermStructure> ts_;
                TermStructureNative(boost::shared_ptr<YieldTermStructure> ts,double spread=0)
                {
                        spreadQuote_=boost::shared_ptr<SimpleQuote>(new SimpleQuote(spread));
                        Handle<Quote> hsq (spreadQuote_);
                        RelinkableHandle<YieldTermStructure> hts(ts);
                        ts_=  boost::shared_ptr<ForwardSpreadedTermStructure>(
                                new ForwardSpreadedTermStructure(hts,hsq ));
                }
}



and finally mixed mode dll

        public ref class TermStructureHandle
        {
        protected:
                        ITermStructureHandleNative* tsHandle_;
        public:
                        property double SpreadBP{ double get() { return tsHandle_->spread()*10000;} void set(double v) {tsHandle_->setSpread(v/10000); }}
                        TermStructureHandle(ITermStructureHandleNative* ts):tsHandle_(ts){}
                        double forwardRate(double t1, double t2, Compounding c ) { return tsHandle_->forwardRate(t1, t2,(int) c);}
                       
                        double forwardRateAct360(DateTime d1, DateTime  d2, Compounding c)
                        {
                                return tsHandle_->forwardRateAct360(
                                        DateTime2QLWrapDate(d1),
                                        DateTime2QLWrapDate(d2),
                                        (int) c);
                        }
                        double forwardRate30360(DateTime d1, DateTime  d2, Compounding c)
                        {
                                return tsHandle_->forwardRate30360(
                                        DateTime2QLWrapDate(d1),
                                        DateTime2QLWrapDate(d2),
                                        (int) c);
                        }

                        double discount(DateTime d1)
                        {
                                return tsHandle_->discount(
                                        DateTime2QLWrapDate(d1));
                        }


                        DateTime maxDate() { return QLWrapDate2DateTime(tsHandle_->maxDate());}
                        int settlementDays() { return tsHandle_->settlementDays();}
                        DateTime settlementDate() { return QLWrapDate2DateTime(tsHandle_->settlementDate());}
                       
                        CalendarHandle^ calendar() { return  gcnew CalendarHandle(tsHandle_-> calendar());}


                !TermStructureHandle()
                {
                        if (tsHandle_!=NULL)
                        {
                                delete tsHandle_;
                                tsHandle_=NULL;
                        }
                }
                ~TermStructureHandle()
                {
                        this->!TermStructureHandle();
                }
               

        };

This approach is the same what Bojan suggested you plus extra dll.

Fischbein, Alan: C12 (NYK) wrote
A 'core' project that is native C++ that uses quantlib; compiled to a static library.
A 'Interface' project that is managed C++, built into a DLL. (so the code is not linked until this project is built.)
An 'add-in' project that is C#/.NET 4.0. The above error is thrown when this project tries to create an instance of a class in Interface.dll