Merge pull request #190 from borglab/fix/generic_value_copy

Fix GenericValue copy
release/4.3a0
Frank Dellaert 2019-12-19 00:21:58 -05:00 committed by GitHub
commit bd51af1b3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 16 deletions

View File

@ -89,12 +89,9 @@ public:
/** /**
* Create a duplicate object returned as a pointer to the generic Value interface. * Create a duplicate object returned as a pointer to the generic Value interface.
* For the sake of performance, this function use singleton pool allocator instead of the normal heap allocator.
* The result must be deleted with Value::deallocate_, not with the 'delete' operator.
*/ */
virtual Value* clone_() const { virtual Value* clone_() const {
void *place = boost::singleton_pool<PoolTag, sizeof(GenericValue)>::malloc(); GenericValue* ptr = new GenericValue(*this); // calls copy constructor to fill in
GenericValue* ptr = new (place) GenericValue(*this); // calls copy constructor to fill in
return ptr; return ptr;
} }
@ -102,8 +99,7 @@ public:
* Destroy and deallocate this object, only if it was originally allocated using clone_(). * Destroy and deallocate this object, only if it was originally allocated using clone_().
*/ */
virtual void deallocate_() const { virtual void deallocate_() const {
this->~GenericValue(); // Virtual destructor cleans up the derived object delete this;
boost::singleton_pool<PoolTag, sizeof(GenericValue)>::free((void*) this); // Release memory from pool
} }
/** /**
@ -118,10 +114,7 @@ public:
// Call retract on the derived class using the retract trait function // Call retract on the derived class using the retract trait function
const T retractResult = traits<T>::Retract(GenericValue<T>::value(), delta); const T retractResult = traits<T>::Retract(GenericValue<T>::value(), delta);
// Create a Value pointer copy of the result Value* resultAsValue = new GenericValue(retractResult);
void* resultAsValuePlace =
boost::singleton_pool<PoolTag, sizeof(GenericValue)>::malloc();
Value* resultAsValue = new (resultAsValuePlace) GenericValue(retractResult);
// Return the pointer to the Value base class // Return the pointer to the Value base class
return resultAsValue; return resultAsValue;
@ -172,12 +165,6 @@ public:
return *this; return *this;
} }
private:
/// Fake Tag struct for singleton pool allocator. In fact, it is never used!
struct PoolTag {
};
private: private:
/** Serialization function */ /** Serialization function */