dnn: fix torch importer memory leaks

pull/9229/head
Alexander Alekhin 8 years ago
parent 0194d5a2d5
commit 878a6906cc
  1. 20
      modules/dnn/src/torch/torch_importer.cpp

@ -100,7 +100,7 @@ struct TorchImporter : public ::cv::dnn::Importer
typedef std::map<String, std::pair<int, Mat> > TensorsMap; typedef std::map<String, std::pair<int, Mat> > TensorsMap;
Net net; Net net;
THFile *file; cv::Ptr<THFile> file;
std::set<int> readedIndexes; std::set<int> readedIndexes;
std::map<int, Mat> storages; std::map<int, Mat> storages;
std::map<int, Mat> tensors; std::map<int, Mat> tensors;
@ -126,7 +126,7 @@ struct TorchImporter : public ::cv::dnn::Importer
rootModule = curModule = NULL; rootModule = curModule = NULL;
moduleCounter = 0; moduleCounter = 0;
file = THDiskFile_new(filename.c_str(), "r", 0); file = cv::Ptr<THFile>(THDiskFile_new(filename.c_str(), "r", 0), THFile_free);
CV_Assert(file && THFile_isOpened(file)); CV_Assert(file && THFile_isOpened(file));
if (isBinary) if (isBinary)
@ -976,18 +976,20 @@ struct TorchImporter : public ::cv::dnn::Importer
{ {
CV_TRACE_FUNCTION(); CV_TRACE_FUNCTION();
if (rootModule == NULL) CV_Assert(rootModule == NULL);
{ cv::Ptr<Module> rootModule_ = cv::makePtr<Module>("Sequential");
rootModule = new Module("Sequential"); rootModule = rootModule_.get();
curModule = rootModule; curModule = rootModule;
THFile_seek(file, 0); THFile_seek(file, 0);
readObject(); readObject();
}
net = net_; net = net_;
std::vector<std::pair<int, Module*> > addedModules; std::vector<std::pair<int, Module*> > addedModules;
fill(rootModule, addedModules); fill(rootModule, addedModules);
rootModule = NULL;
curModule = NULL;
} }
}; };

Loading…
Cancel
Save