diff --git a/modules/dnn/src/torch/torch_importer.cpp b/modules/dnn/src/torch/torch_importer.cpp index 7c1d4e7647..44fcd8c24c 100644 --- a/modules/dnn/src/torch/torch_importer.cpp +++ b/modules/dnn/src/torch/torch_importer.cpp @@ -100,7 +100,7 @@ struct TorchImporter : public ::cv::dnn::Importer typedef std::map > TensorsMap; Net net; - THFile *file; + cv::Ptr file; std::set readedIndexes; std::map storages; std::map tensors; @@ -126,7 +126,7 @@ struct TorchImporter : public ::cv::dnn::Importer rootModule = curModule = NULL; moduleCounter = 0; - file = THDiskFile_new(filename.c_str(), "r", 0); + file = cv::Ptr(THDiskFile_new(filename.c_str(), "r", 0), THFile_free); CV_Assert(file && THFile_isOpened(file)); if (isBinary) @@ -976,18 +976,20 @@ struct TorchImporter : public ::cv::dnn::Importer { CV_TRACE_FUNCTION(); - if (rootModule == NULL) - { - rootModule = new Module("Sequential"); - curModule = rootModule; + CV_Assert(rootModule == NULL); + cv::Ptr rootModule_ = cv::makePtr("Sequential"); + rootModule = rootModule_.get(); + curModule = rootModule; - THFile_seek(file, 0); - readObject(); - } + THFile_seek(file, 0); + readObject(); net = net_; std::vector > addedModules; fill(rootModule, addedModules); + + rootModule = NULL; + curModule = NULL; } };