From fcedee0abb28c4562caf05085274ad06729be597 Mon Sep 17 00:00:00 2001 From: Rok Mandeljc Date: Thu, 1 Oct 2015 22:24:29 +0200 Subject: [PATCH] opencv_dnn: replaced static LayerFactory::impl member with a static function that constructs the object on the first use (fix for bug #383) This way, we are certain not to depend on the order of static variable initialization (LayerFactory::impl in dnn.cpp, vs. init in init.cpp), which is causing bug #383. --- modules/dnn/include/opencv2/dnn/layer.hpp | 2 +- modules/dnn/src/dnn.cpp | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/modules/dnn/include/opencv2/dnn/layer.hpp b/modules/dnn/include/opencv2/dnn/layer.hpp index b9acc3144..b28b6acd8 100644 --- a/modules/dnn/include/opencv2/dnn/layer.hpp +++ b/modules/dnn/include/opencv2/dnn/layer.hpp @@ -77,7 +77,7 @@ private: LayerFactory(); struct Impl; - static Ptr impl; + static Ptr impl(); }; /** @brief Registers layer constructor in runtime. diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index 08308e252..7616aa85c 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -561,34 +561,38 @@ struct LayerFactory::Impl : public std::map { }; -//allocates on load and cleans on exit -Ptr LayerFactory::impl(new LayerFactory::Impl()); +Ptr LayerFactory::impl () +{ + // allocate on first use + static Ptr impl_(new LayerFactory::Impl()); + return impl_; +} void LayerFactory::registerLayer(const String &_type, Constuctor constructor) { String type = _type.toLowerCase(); - Impl::iterator it = impl->find(type); + Impl::iterator it = impl()->find(type); - if (it != impl->end() && it->second != constructor) + if (it != impl()->end() && it->second != constructor) { CV_Error(cv::Error::StsBadArg, "Layer \"" + type + "\" already was registered"); } - impl->insert(std::make_pair(type, constructor)); + impl()->insert(std::make_pair(type, constructor)); } void LayerFactory::unregisterLayer(const String &_type) { String type = _type.toLowerCase(); - impl->erase(type); + impl()->erase(type); } Ptr LayerFactory::createLayerInstance(const String &_type, LayerParams& params) { String type = _type.toLowerCase(); - Impl::const_iterator it = LayerFactory::impl->find(type); + Impl::const_iterator it = LayerFactory::impl()->find(type); - if (it != impl->end()) + if (it != impl()->end()) { return it->second(params); }