diff --git a/modules/dnn/include/opencv2/dnn/layer.hpp b/modules/dnn/include/opencv2/dnn/layer.hpp
index b9acc31442081a7736924571ecea40b11e0ccf8c..b28b6acd88a2647be8001c625fafb19739a9ded7 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> impl;
+    static Ptr<Impl> impl();
 };
 
 /** @brief Registers layer constructor in runtime.
diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp
index 08308e252832416c3a4494b79395c36865f76e7a..7616aa85c84a47b0cc9a29a43a21f2be0cf285dd 100644
--- a/modules/dnn/src/dnn.cpp
+++ b/modules/dnn/src/dnn.cpp
@@ -561,34 +561,38 @@ struct LayerFactory::Impl : public std::map<String, LayerFactory::Constuctor>
 {
 };
 
-//allocates on load and cleans on exit
-Ptr<LayerFactory::Impl> LayerFactory::impl(new LayerFactory::Impl());
+Ptr<LayerFactory::Impl> LayerFactory::impl ()
+{
+    // allocate on first use
+    static Ptr<LayerFactory::Impl> 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<Layer> 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);
     }