http://quantlib.414.s1.nabble.com/Excel-2007-crashes-on-exit-after-QLXL-call-that-returns-an-object-tp5666p5669.html
Hi Eric -
Here is a patch which I believe fixes the problem. I think you can recreate the problem by running Excel 2007 from the visual studio debugger after having built the static debug xll; Load your newly built addin; Call a quantlib function that returns an object and Exit Excel. You should see an access violation.
I think I misspoke a little when I described what causes Excel to fail. It seems to have something to do with some facilities not being available after a certain point as Excel shuts down. In this case, Excel calls xlAutoClose and well after it finishes the normal C++ exit proceeds and the xll calls its own destructors including one for callingRanges_; the destructor for CallingRange calls xlfSetName but those calls are no longer available. As a result, they seem to seg fault.
This patch forces the last of the QuantLib's xlfSetName calls to happen before xlAutoClose finishes in a relatively simple way.
Best,
Dave
Index: QuantLibXL/qlxl/register/addin.cpp
===================================================================
--- QuantLibXL/qlxl/register/addin.cpp (revision 113)
+++ QuantLibXL/qlxl/register/addin.cpp (working copy)
@@ -153,6 +153,10 @@
// Unregister QuantLib functions
unregisterQlFunctions(xDll);
+ // Clean objects out of the repository before we close the xll
+ // since their destructors call Excel(xlfSetName)
+ ObjectHandler::RepositoryXL::instance().clearCallingRangeCache();
+
// Deallocate the Enumeration Registry
QuantLibAddin::unregisterEnumerations();
Index: ObjectHandler/ohxl/register/addin.cpp
===================================================================
--- ObjectHandler/ohxl/register/addin.cpp (revision 113)
+++ ObjectHandler/ohxl/register/addin.cpp (working copy)
@@ -76,6 +76,8 @@
// unregister the addin functions
Excel(xlGetName, &xDll, 0);
unregisterOhFunctions(xDll);
+
+ ObjectHandler::RepositoryXL::instance().clearCallingRangeCache();
Excel(xlFree, 0, 1, &xDll);
return 1;
Index: ObjectHandler/ohxl/repositoryxl.cpp
===================================================================
--- ObjectHandler/ohxl/repositoryxl.cpp (revision 113)
+++ ObjectHandler/ohxl/repositoryxl.cpp (working copy)
@@ -56,6 +56,10 @@
typedef std::map<std::string, boost::shared_ptr<CallingRange> > RangeMap;
RangeMap callingRanges_;
+ void RepositoryXL::clearCallingRangeCache() {
+ callingRanges_.clear();
+ }
+
RepositoryXL &RepositoryXL::instance() {
if (instance_) {
RepositoryXL *ret = dynamic_cast<RepositoryXL*>(instance_);
Index: ObjectHandler/ohxl/repositoryxl.hpp
===================================================================
--- ObjectHandler/ohxl/repositoryxl.hpp (revision 113)
+++ ObjectHandler/ohxl/repositoryxl.hpp (working copy)
@@ -51,6 +51,11 @@
virtual std::string storeObject(const std::string &objectID,
const boost::shared_ptr<Object> &obj,
bool overwrite = false);
+ //@{
+ //! Method to cleanup local variables before exit.
+ /*! Clears the CallingRange cache
+ */
+ virtual void clearCallingRangeCache();
//@}
//! \name Error Messages
> Subject: Re: [Quantlib-users] Excel 2007 crashes on exit after QLXL call that returns an object
> From:
[hidden email]> To:
[hidden email]> CC:
[hidden email]> Date: Fri, 9 Dec 2011 11:21:12 +0100
>
> Hi Dave,
>
> On Thu, 2011-12-08 at 13:27 -0800, Dave Blob wrote:
> > I think I've found the problem-
> >
> > It looks like when CallingRanges are destroyed, they call Excel(xlfSetName,
> > ...). However I see CallingRanges being destroyed after xlAutoClose, which
> > calls Excel(xlfFree). I think this causes the seg faults.
> >
> > To fix the problem I added ReposityXL::clear() - which merely calls
> > calllingNames_.clear(). I call
> > ObjectHandler::ReposityXL::instance().clear() before Excel(xlfFree) - and
> > the access violations seem to have gone away. I can submit a patch if you
> > think this it is helpful (right now I have a patch for a heavily stripped
> > down version that I used to isolate the bug).
>
> That would be much appreciated if you could send a patch. When I
> package up the next release I will try to recreate the bug and test your
> fix.
>
> Kind Regards,
> Eric
>
developers. It will provide a great way to learn Windows Azure and what it
provides. You can attend the event by watching it streamed LIVE online.