Skip to content

Commit f0c56f0

Browse files
committed
Making a copy of the dataset in when building the index in the Matlab mex file to fix the issue of Matlab releasing the memory after index was built.
1 parent 98c9226 commit f0c56f0

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/matlab/nearest_neighbors.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct TypedIndex
4242
{
4343
flann_index_t index;
4444
flann_datatype_t type;
45+
void* dataset;
4546
};
4647

4748

@@ -332,6 +333,15 @@ static void _index_find_nn(int nOutArray, mxArray* OutArray[], int nInArray, con
332333
}
333334

334335

336+
template<typename T>
337+
T* copy_array(const mxArray* array)
338+
{
339+
size_t mem_size = mxGetN(array)*mxGetM(array)*sizeof(T);
340+
void* data = malloc(mem_size);
341+
memcpy(data,mxGetData(array),mem_size);
342+
return (T*)data;
343+
}
344+
335345
/**
336346
* Input arguments: dataset (matrix), params (struct)
337347
* Output arguments: index (pointer to index), params (struct), speedup(double)
@@ -349,6 +359,8 @@ static void _build_index(int nOutArray, mxArray* OutArray[], int nInArray, const
349359
const mxArray* datasetMat = InArray[0];
350360
check_allowed_type(datasetMat);
351361

362+
363+
352364
int dcount = mxGetN(datasetMat);
353365
int length = mxGetM(datasetMat);
354366

@@ -364,24 +376,28 @@ static void _build_index(int nOutArray, mxArray* OutArray[], int nInArray, const
364376
TypedIndex* typedIndex = new TypedIndex();
365377

366378
if (mxIsSingle(datasetMat)) {
367-
float* dataset = (float*) mxGetData(datasetMat);
379+
float* dataset = copy_array<float>(datasetMat);
368380
typedIndex->index = flann_build_index_float(dataset,dcount,length, &speedup, &p);
369381
typedIndex->type = FLANN_FLOAT32;
382+
typedIndex->dataset = dataset;
370383
}
371384
else if (mxIsDouble(datasetMat)) {
372-
double* dataset = (double*) mxGetData(datasetMat);
385+
double* dataset = copy_array<double>(datasetMat);
373386
typedIndex->index = flann_build_index_double(dataset,dcount,length, &speedup, &p);
374387
typedIndex->type = FLANN_FLOAT64;
388+
typedIndex->dataset = dataset;
375389
}
376390
else if (mxIsUint8(datasetMat)) {
377-
unsigned char* dataset = (unsigned char*) mxGetData(datasetMat);
391+
unsigned char* dataset = copy_array<unsigned char>(datasetMat);
378392
typedIndex->index = flann_build_index_byte(dataset,dcount,length, &speedup, &p);
379393
typedIndex->type = FLANN_UINT8;
394+
typedIndex->dataset = dataset;
380395
}
381396
else if (mxIsInt32(datasetMat)) {
382-
int* dataset = (int*) mxGetData(datasetMat);
397+
int* dataset = copy_array<int>(datasetMat);
383398
typedIndex->index = flann_build_index_int(dataset,dcount,length, &speedup, &p);
384399
typedIndex->type = FLANN_INT32;
400+
typedIndex->dataset = dataset;
385401
}
386402

387403
mxClassID classID;
@@ -432,6 +448,7 @@ static void _free_index(int nOutArray, mxArray* OutArray[], int nInArray, const
432448
else if (typedIndex->type==FLANN_INT32) {
433449
flann_free_index_int(typedIndex->index, NULL);
434450
}
451+
free(typedIndex->dataset);
435452
delete typedIndex;
436453
}
437454

@@ -543,24 +560,28 @@ static void _load_index(int nOutArray, mxArray* OutArray[], int nInArray, const
543560
TypedIndex* typedIndex = new TypedIndex();
544561

545562
if (mxIsSingle(datasetMat)) {
546-
float* dataset = (float*) mxGetData(datasetMat);
563+
float* dataset = copy_array<float>(datasetMat);
547564
typedIndex->index = flann_load_index_float(filename, dataset,dcount,length);
548565
typedIndex->type = FLANN_FLOAT32;
566+
typedIndex->dataset = dataset;
549567
}
550568
else if (mxIsDouble(datasetMat)) {
551-
double* dataset = (double*) mxGetData(datasetMat);
569+
double* dataset = copy_array<double>(datasetMat);
552570
typedIndex->index = flann_load_index_double(filename, dataset,dcount,length);
553571
typedIndex->type = FLANN_FLOAT64;
572+
typedIndex->dataset = dataset;
554573
}
555574
else if (mxIsUint8(datasetMat)) {
556-
unsigned char* dataset = (unsigned char*) mxGetData(datasetMat);
575+
unsigned char* dataset = copy_array<unsigned char>(datasetMat);
557576
typedIndex->index = flann_load_index_byte(filename, dataset,dcount,length);
558577
typedIndex->type = FLANN_UINT8;
578+
typedIndex->dataset = dataset;
559579
}
560580
else if (mxIsInt32(datasetMat)) {
561-
int* dataset = (int*) mxGetData(datasetMat);
581+
int* dataset = copy_array<int>(datasetMat);
562582
typedIndex->index = flann_load_index_int(filename, dataset,dcount,length);
563583
typedIndex->type = FLANN_INT32;
584+
typedIndex->dataset = dataset;
564585
}
565586

566587
mxClassID classID;

0 commit comments

Comments
 (0)