fixed memory leak in flann index
authorIlya Lavrenov <ilya.lavrenov@itseez.com>
Tue, 1 Sep 2015 13:17:18 +0000 (16:17 +0300)
committerIlya Lavrenov <ilya.lavrenov@itseez.com>
Tue, 1 Sep 2015 13:58:35 +0000 (16:58 +0300)
modules/flann/src/miniflann.cpp

index 5ce3e90..9bf5033 100644 (file)
@@ -318,12 +318,14 @@ buildIndex_(void*& index, const Mat& wholedata, const Mat& data, const IndexPara
 
     ::cvflann::Matrix<ElementType> dataset((ElementType*)data.data, data.rows, data.cols);
 
-    IndexType* _index = NULL;
-    if( !index || getParam<flann_algorithm_t>(params, "algorithm", FLANN_INDEX_LINEAR) != FLANN_INDEX_LSH) // currently, additional index support is the lsh algorithm only.
+    // currently, additional index support is the lsh algorithm only.
+    if( !index || getParam<flann_algorithm_t>(params, "algorithm", FLANN_INDEX_LINEAR) != FLANN_INDEX_LSH)
     {
-        _index = new IndexType(dataset, get_params(params), dist);
+        Ptr<IndexType> _index = makePtr<IndexType>(dataset, get_params(params), dist);
         _index->buildIndex();
         index = _index;
+        // HACK to prevent object destruction
+        _index.obj = NULL;
     }
     else // build additional lsh index
     {