Skip to content

Commit 878a690

Browse files
committed
dnn: fix torch importer memory leaks
1 parent 0194d5a commit 878a690

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

modules/dnn/src/torch/torch_importer.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct TorchImporter : public ::cv::dnn::Importer
100100
typedef std::map<String, std::pair<int, Mat> > TensorsMap;
101101
Net net;
102102

103-
THFile *file;
103+
cv::Ptr<THFile> file;
104104
std::set<int> readedIndexes;
105105
std::map<int, Mat> storages;
106106
std::map<int, Mat> tensors;
@@ -126,7 +126,7 @@ struct TorchImporter : public ::cv::dnn::Importer
126126
rootModule = curModule = NULL;
127127
moduleCounter = 0;
128128

129-
file = THDiskFile_new(filename.c_str(), "r", 0);
129+
file = cv::Ptr<THFile>(THDiskFile_new(filename.c_str(), "r", 0), THFile_free);
130130
CV_Assert(file && THFile_isOpened(file));
131131

132132
if (isBinary)
@@ -976,18 +976,20 @@ struct TorchImporter : public ::cv::dnn::Importer
976976
{
977977
CV_TRACE_FUNCTION();
978978

979-
if (rootModule == NULL)
980-
{
981-
rootModule = new Module("Sequential");
982-
curModule = rootModule;
979+
CV_Assert(rootModule == NULL);
980+
cv::Ptr<Module> rootModule_ = cv::makePtr<Module>("Sequential");
981+
rootModule = rootModule_.get();
982+
curModule = rootModule;
983983

984-
THFile_seek(file, 0);
985-
readObject();
986-
}
984+
THFile_seek(file, 0);
985+
readObject();
987986

988987
net = net_;
989988
std::vector<std::pair<int, Module*> > addedModules;
990989
fill(rootModule, addedModules);
990+
991+
rootModule = NULL;
992+
curModule = NULL;
991993
}
992994
};
993995

0 commit comments

Comments
 (0)