diff --git a/gtsam/base/types.h b/gtsam/base/types.h index 0488c15c0..d4d39af4b 100644 --- a/gtsam/base/types.h +++ b/gtsam/base/types.h @@ -31,6 +31,7 @@ #ifdef GTSAM_USE_TBB #include +#include #endif namespace gtsam { @@ -148,16 +149,21 @@ namespace gtsam { public std::exception #endif { - private: #ifdef GTSAM_USE_TBB + private: typedef tbb::tbb_exception Base; + protected: + typedef std::basic_string, tbb::tbb_allocator > String; #else + private: typedef std::exception Base; + protected: + typedef std::string String; #endif protected: bool dynamic_; ///< Whether this object was moved - boost::optional description_; ///< Optional description + mutable boost::optional description_; ///< Optional description /// Default constructor is protected - may only be created from derived classes ThreadsafeException() : dynamic_(false) {} @@ -166,7 +172,7 @@ namespace gtsam { ThreadsafeException(const ThreadsafeException& other) : Base(other), dynamic_(false) {} /// Construct with description string - ThreadsafeException(const std::string& description) : dynamic_(false), description_(description) {} + ThreadsafeException(const std::string& description) : dynamic_(false), description_(String(description.begin(), description.end())) {} /// Default destructor doesn't have the throw() virtual ~ThreadsafeException() throw () {} @@ -175,14 +181,22 @@ namespace gtsam { // Implement functions for tbb_exception #ifdef GTSAM_USE_TBB virtual tbb::tbb_exception* move() throw () { - DERIVED* clone = ::new DERIVED(static_cast(*this)); + void* cloneMemory = scalable_malloc(sizeof(DERIVED)); + if (!cloneMemory) { + std::cerr << "Failed allocating memory to copy thrown exception, exiting now." << std::endl; + exit(-1); + } + DERIVED* clone = ::new(cloneMemory) DERIVED(static_cast(*this)); clone->dynamic_ = true; return clone; } virtual void destroy() throw() { - if (dynamic_) - ::delete this; + if (dynamic_) { + DERIVED* derivedPtr = static_cast(this); + derivedPtr->~DERIVED(); + scalable_free(derivedPtr); + } } virtual void throw_self() { diff --git a/gtsam/linear/linearExceptions.h b/gtsam/linear/linearExceptions.h index 812015042..92aefa7f8 100644 --- a/gtsam/linear/linearExceptions.h +++ b/gtsam/linear/linearExceptions.h @@ -94,23 +94,22 @@ namespace gtsam { */ class IndeterminantLinearSystemException : public ThreadsafeException { Index j_; - mutable std::string what_; public: IndeterminantLinearSystemException(Index j) throw() : j_(j) {} virtual ~IndeterminantLinearSystemException() throw() {} Index nearbyVariable() const { return j_; } virtual const char* what() const throw() { - if(what_.empty()) - what_ = + if(!description_) + description_ = String( "\nIndeterminant linear system detected while working near variable with\n" -"index " + boost::lexical_cast(j_) + " in ordering.\n" +"index " + boost::lexical_cast(j_) + " in ordering.\n" "\n\ Thrown when a linear system is ill-posed. The most common cause for this\n\ error is having underconstrained variables. Mathematically, the system is\n\ either underdetermined, or its quadratic error function is concave in some\n\ directions. See the GTSAM Doxygen documentation at http://borg.cc.gatech.edu/ \n\ -on gtsam::IndeterminantLinearSystemException for more information.\n"; - return what_.c_str(); +on gtsam::IndeterminantLinearSystemException for more information.\n"); + return description_->c_str(); } };