diff --git a/modules/viz/src/precomp.hpp b/modules/viz/src/precomp.hpp index 57b421dbbc..ac8c67c1bd 100644 --- a/modules/viz/src/precomp.hpp +++ b/modules/viz/src/precomp.hpp @@ -149,8 +149,8 @@ namespace cv VizStorage(); // Static ~VizStorage(); - static void add(Viz3d window); - static Viz3d get(const String &window_name); + static void add(const Viz3d& window); + static Viz3d& get(const String &window_name); static void remove(const String &window_name); static bool windowExists(const String &window_name); static void removeUnreferenced(); diff --git a/modules/viz/src/viz.cpp b/modules/viz/src/viz.cpp index 4b88d7d185..6a08dfa34a 100644 --- a/modules/viz/src/viz.cpp +++ b/modules/viz/src/viz.cpp @@ -112,7 +112,7 @@ namespace cv { namespace viz cv::viz::VizMap cv::viz::VizStorage::storage; void cv::viz::VizStorage::unregisterAll() { storage.clear(); } -cv::viz::Viz3d cv::viz::VizStorage::get(const String &window_name) +cv::viz::Viz3d& cv::viz::VizStorage::get(const String &window_name) { String name = generateWindowName(window_name); VizMap::iterator vm_itr = storage.find(name); @@ -120,7 +120,7 @@ cv::viz::Viz3d cv::viz::VizStorage::get(const String &window_name) return vm_itr->second; } -void cv::viz::VizStorage::add(Viz3d window) +void cv::viz::VizStorage::add(const Viz3d& window) { String window_name = window.getWindowName(); VizMap::iterator vm_itr = storage.find(window_name); @@ -136,9 +136,11 @@ bool cv::viz::VizStorage::windowExists(const String &window_name) void cv::viz::VizStorage::removeUnreferenced() { - for(VizMap::iterator pos = storage.begin(); pos != storage.end(); ++pos) + for(VizMap::iterator pos = storage.begin(); pos != storage.end();) if(pos->second.impl_->ref_counter == 1) - storage.erase(pos); + storage.erase(pos++); + else + ++pos; } cv::String cv::viz::VizStorage::generateWindowName(const String &window_name) @@ -159,5 +161,5 @@ cv::String cv::viz::VizStorage::generateWindowName(const String &window_name) return output; } -cv::viz::Viz3d cv::viz::get(const String &window_name) { return Viz3d(window_name); } +cv::viz::Viz3d cv::viz::get(const String &window_name) { return Viz3d (window_name); } void cv::viz::unregisterAllWindows() { VizStorage::unregisterAll(); } diff --git a/modules/viz/src/viz3d.cpp b/modules/viz/src/viz3d.cpp index ec57a3f44c..08cb880dec 100644 --- a/modules/viz/src/viz3d.cpp +++ b/modules/viz/src/viz3d.cpp @@ -62,7 +62,8 @@ cv::viz::Viz3d& cv::viz::Viz3d::operator=(const Viz3d& other) { release(); impl_ = other.impl_; - if (impl_) CV_XADD(&impl_->ref_counter, 1); + if (impl_) + CV_XADD(&impl_->ref_counter, 1); } return *this; } @@ -89,10 +90,15 @@ void cv::viz::Viz3d::create(const String &window_name) void cv::viz::Viz3d::release() { if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1) + { delete impl_; + impl_ = 0; + } + + if (impl_ && impl_->ref_counter == 1) + VizStorage::removeUnreferenced(); impl_ = 0; - VizStorage::removeUnreferenced(); } void cv::viz::Viz3d::spin() { impl_->spin(); }