From 4b443059ec5a5cf33bd2a55be6dd3e137579d743 Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Wed, 28 Aug 2013 21:27:30 +0200 Subject: [PATCH] reverted widget reference count in order to avoid memory leak --- modules/viz/include/opencv2/viz/widgets.hpp | 6 +++ modules/viz/src/widget.cpp | 43 ++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index caaee9e698..01ed3ea8da 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -14,6 +14,9 @@ namespace cv { public: Widget(); + Widget(const Widget& other); + Widget& operator=(const Widget& other); + ~Widget(); static Widget fromPlyFile(const String &file_name); @@ -25,6 +28,9 @@ namespace cv class Impl; Impl *impl_; friend struct WidgetAccessor; + + void create(); + void release(); }; ///////////////////////////////////////////////////////////////////////////// diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index 485e35ed74..b3f1479781 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -7,11 +7,52 @@ class cv::viz::Widget::Impl { public: vtkSmartPointer prop; + int ref_counter; Impl() : prop(0) {} }; -cv::viz::Widget::Widget() : impl_( new Impl() ) { } +cv::viz::Widget::Widget() : impl_(0) +{ + create(); +} + +cv::viz::Widget::Widget(const Widget& other) : impl_(other.impl_) +{ + if (impl_) CV_XADD(&impl_->ref_counter, 1); +} + +cv::viz::Widget& cv::viz::Widget::operator=(const Widget& other) +{ + if (this != &other) + { + release(); + impl_ = other.impl_; + if (impl_) CV_XADD(&impl_->ref_counter, 1); + } + return *this; +} + +cv::viz::Widget::~Widget() +{ + release(); +} + +void cv::viz::Widget::create() +{ + if (impl_) release(); + impl_ = new Impl(); + impl_->ref_counter = 1; +} + +void cv::viz::Widget::release() +{ + if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1) + { + delete impl_; + impl_ = 0; + } +} cv::viz::Widget cv::viz::Widget::fromPlyFile(const String &file_name) {