diff --git a/src/OpenMesh/Core/Mesh/ArrayKernel.cc b/src/OpenMesh/Core/Mesh/ArrayKernel.cc index 8a24f3ac..64d7f807 100644 --- a/src/OpenMesh/Core/Mesh/ArrayKernel.cc +++ b/src/OpenMesh/Core/Mesh/ArrayKernel.cc @@ -257,14 +257,20 @@ void ArrayKernel::garbage_collection(bool _v, bool _e, bool _f) void ArrayKernel::clear() { - vertices_.clear(); - edges_.clear(); - faces_.clear(); + vprops_clear(); + eprops_clear(); + hprops_clear(); + fprops_clear(); + + vertices_.clear(); + VertexContainer().swap( vertices_ ); + + edges_.clear(); + EdgeContainer().swap( edges_ ); + + faces_.clear(); + FaceContainer().swap( faces_ ); - vprops_resize(0); - eprops_resize(0); - hprops_resize(0); - fprops_resize(0); } void ArrayKernel::resize( uint _n_vertices, uint _n_edges, uint _n_faces ) diff --git a/src/OpenMesh/Core/Mesh/BaseKernel.hh b/src/OpenMesh/Core/Mesh/BaseKernel.hh index cb96a53a..e791580e 100644 --- a/src/OpenMesh/Core/Mesh/BaseKernel.hh +++ b/src/OpenMesh/Core/Mesh/BaseKernel.hh @@ -93,7 +93,12 @@ class BaseKernel public: //-------------------------------------------- constructor / destructor BaseKernel() {} - virtual ~BaseKernel() {} + virtual ~BaseKernel() { + vprops_.clear(); + eprops_.clear(); + hprops_.clear(); + fprops_.clear(); + } public: //-------------------------------------------------- add new properties @@ -504,29 +509,44 @@ protected: //------------------------------------------- synchronize properties void vprops_reserve(unsigned int _n) const { vprops_.reserve(_n); } void vprops_resize(unsigned int _n) const { vprops_.resize(_n); } + void vprops_clear() { + vprops_.clear(); + } void vprops_swap(unsigned int _i0, unsigned int _i1) const { vprops_.swap(_i0, _i1); } void hprops_reserve(unsigned int _n) const { hprops_.reserve(_n); } void hprops_resize(unsigned int _n) const { hprops_.resize(_n); } + void hprops_clear() { + hprops_.clear(); + } void hprops_swap(unsigned int _i0, unsigned int _i1) const { hprops_.swap(_i0, _i1); } void eprops_reserve(unsigned int _n) const { eprops_.reserve(_n); } void eprops_resize(unsigned int _n) const { eprops_.resize(_n); } + void eprops_clear() { + eprops_.clear(); + } void eprops_swap(unsigned int _i0, unsigned int _i1) const { eprops_.swap(_i0, _i1); } void fprops_reserve(unsigned int _n) const { fprops_.reserve(_n); } void fprops_resize(unsigned int _n) const { fprops_.resize(_n); } + void fprops_clear() { + fprops_.clear(); + } void fprops_swap(unsigned int _i0, unsigned int _i1) const { fprops_.swap(_i0, _i1); } void mprops_resize(unsigned int _n) const { mprops_.resize(_n); } + void mprops_clear() { + mprops_.clear(); + } public: diff --git a/src/OpenMesh/Core/Utils/BaseProperty.hh b/src/OpenMesh/Core/Utils/BaseProperty.hh index ead16e42..77116ceb 100644 --- a/src/OpenMesh/Core/Utils/BaseProperty.hh +++ b/src/OpenMesh/Core/Utils/BaseProperty.hh @@ -97,6 +97,9 @@ public: // synchronized array interface /// Resize storage to hold n elements. virtual void resize(size_t _n) = 0; + /// Clear all elements and free memory. + virtual void clear() = 0; + /// Extend the number of elements by one. virtual void push_back() = 0; diff --git a/src/OpenMesh/Core/Utils/Property.hh b/src/OpenMesh/Core/Utils/Property.hh index fc46b33e..f828fcad 100644 --- a/src/OpenMesh/Core/Utils/Property.hh +++ b/src/OpenMesh/Core/Utils/Property.hh @@ -109,6 +109,7 @@ public: // inherited from BaseProperty virtual void reserve(size_t _n) { data_.reserve(_n); } virtual void resize(size_t _n) { data_.resize(_n); } + virtual void clear() { data_.clear(); vector_type().swap(data_); } virtual void push_back() { data_.push_back(T()); } virtual void swap(size_t _i0, size_t _i1) { std::swap(data_[_i0], data_[_i1]); } @@ -161,18 +162,18 @@ public: public: // data access interface /// Get pointer to array (does not work for T==bool) - const T* data() const { + const T* data() const { if( data_.empty() ) return 0; - return &data_[0]; + return &data_[0]; } /// Get reference to property vector (be careful, improper usage, e.g. resizing, may crash OpenMesh!!!) - vector_type& data_vector() { + vector_type& data_vector() { - return data_; + return data_; } /// Access the i'th element. No range check is performed! @@ -230,6 +231,7 @@ public: // inherited from BaseProperty virtual void reserve(size_t _n) { data_.reserve(_n); } virtual void resize(size_t _n) { data_.resize(_n); } + virtual void clear() { data_.clear(); vector_type().swap(data_); } virtual void push_back() { data_.push_back(bool()); } virtual void swap(size_t _i0, size_t _i1) { bool t(data_[_i0]); data_[_i0]=data_[_i1]; data_[_i1]=t; } @@ -387,6 +389,7 @@ public: // inherited from BaseProperty virtual void reserve(size_t _n) { data_.reserve(_n); } virtual void resize(size_t _n) { data_.resize(_n); } + virtual void clear() { data_.clear(); vector_type().swap(data_); } virtual void push_back() { data_.push_back(std::string()); } virtual void swap(size_t _i0, size_t _i1) { std::swap(data_[_i0], data_[_i1]); diff --git a/src/OpenMesh/Core/Utils/PropertyContainer.hh b/src/OpenMesh/Core/Utils/PropertyContainer.hh index abf0d214..dcd53bd8 100644 --- a/src/OpenMesh/Core/Utils/PropertyContainer.hh +++ b/src/OpenMesh/Core/Utils/PropertyContainer.hh @@ -64,7 +64,7 @@ public: //-------------------------------------------------- constructor / destructor PropertyContainer() {} - virtual ~PropertyContainer() { clear(); } + virtual ~PropertyContainer() { std::for_each(properties_.begin(), properties_.end(), Delete()); } //------------------------------------------------------------- info / access @@ -177,7 +177,16 @@ public: void clear() { - std::for_each(properties_.begin(), properties_.end(), Delete()); + // Clear properties vector: + // Replaced the old version with new one + // which performs a swap to clear values and + // deallocate memory. + + // Old version (changed 22.07.09) { + // std::for_each(properties_.begin(), properties_.end(), Delete()); + // } + + std::for_each(properties_.begin(), properties_.end(), ClearAll()); } @@ -256,6 +265,12 @@ private: size_t n_; }; + struct ClearAll + { + ClearAll() {} + void operator()(BaseProperty* _p) const { if (_p) _p->clear(); } + }; + struct Swap { Swap(size_t _i0, size_t _i1) : i0_(_i0), i1_(_i1) {}