1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

Merge pull request #23276 from vovka643:flann_corrections

Fixed potential memory leak in flann

Issue #22426

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Vladimir Ponomarev
2023-03-14 15:00:44 +03:00
committed by GitHub
parent 9f2182abbb
commit b204c39815
2 changed files with 23 additions and 10 deletions
@@ -45,6 +45,21 @@
namespace cvflann
{
class FILEScopeGuard {
public:
explicit FILEScopeGuard(FILE* file) {
file_ = file;
};
~FILEScopeGuard() {
fclose(file_);
};
private:
FILE* file_;
};
/**
* Sets the log level used for all flann functions
@@ -69,7 +84,6 @@ struct SavedIndexParams : public IndexParams
}
};
template<typename Distance>
NNIndex<Distance>* load_saved_index(const Matrix<typename Distance::ElementType>& dataset, const cv::String& filename, Distance distance)
{
@@ -79,13 +93,13 @@ NNIndex<Distance>* load_saved_index(const Matrix<typename Distance::ElementType>
if (fin == NULL) {
return NULL;
}
FILEScopeGuard fscgd(fin);
IndexHeader header = load_header(fin);
if (header.data_type != Datatype<ElementType>::type()) {
fclose(fin);
FLANN_THROW(cv::Error::StsError, "Datatype of saved index is different than of the one to be created.");
}
if ((size_t(header.rows) != dataset.rows)||(size_t(header.cols) != dataset.cols)) {
fclose(fin);
FLANN_THROW(cv::Error::StsError, "The index saved belongs to a different dataset");
}
@@ -93,7 +107,6 @@ NNIndex<Distance>* load_saved_index(const Matrix<typename Distance::ElementType>
params["algorithm"] = header.index_type;
NNIndex<Distance>* nnIndex = create_index_by_type<Distance>(dataset, params, distance);
nnIndex->loadIndex(fin);
fclose(fin);
return nnIndex;
}
@@ -107,7 +120,7 @@ public:
typedef typename Distance::ResultType DistanceType;
Index(const Matrix<ElementType>& features, const IndexParams& params, Distance distance = Distance() )
: index_params_(params)
:index_params_(params)
{
flann_algorithm_t index_type = get_param<flann_algorithm_t>(params,"algorithm");
loaded_ = false;
+5 -5
View File
@@ -767,11 +767,15 @@ bool Index::load(InputArray _data, const String& filename)
Mat data = _data.getMat();
bool ok = true;
release();
FILE* fin = fopen(filename.c_str(), "rb");
if (fin == NULL)
if (fin == NULL) {
return false;
}
FILEScopeGuard fscgd(fin);
::cvflann::IndexHeader header = ::cvflann::load_header(fin);
algo = header.index_type;
featureType = header.data_type == FLANN_UINT8 ? CV_8U :
header.data_type == FLANN_INT8 ? CV_8S :
@@ -786,7 +790,6 @@ bool Index::load(InputArray _data, const String& filename)
{
fprintf(stderr, "Reading FLANN index error: the saved data size (%d, %d) or type (%d) is different from the passed one (%d, %d), %d\n",
(int)header.rows, (int)header.cols, featureType, data.rows, data.cols, data.type());
fclose(fin);
return false;
}
@@ -799,7 +802,6 @@ bool Index::load(InputArray _data, const String& filename)
(distType != FLANN_DIST_HAMMING && featureType == CV_32F)) )
{
fprintf(stderr, "Reading FLANN index error: unsupported feature type %d for the index type %d\n", featureType, algo);
fclose(fin);
return false;
}
@@ -839,8 +841,6 @@ bool Index::load(InputArray _data, const String& filename)
ok = false;
}
if( fin )
fclose(fin);
return ok;
}