mariusmuja/flann — Issue #32: MATLAB Bug
Author: davidt0x
Created: 2012-03-02 19:42:45+00:00
State: open

Hey,

First off, thanks for a great library! You guys are providing an invaluable resource so please keep it.

That being said, I just want to bring to your attention a bug that I believe I found in the matlab interface. While using the mex interface I kept getting seg faults that would seemingly occur at random. It seemed like they would occur more frequently when working with larger amounts of data. I believe I have tracked the problem down to the improper use of pointers when building or loading and index. The functions _build_index and _load_index within the nearest_neighbors.cpp code both pass a pointer of the dataset to the API when constructing and index. However, I see that the FLANN code does not perform a copy of the dataset when constructing the index. This was probably intended behavior I imagine. However, this causes problems when trying to make the index persistent accross mex calls because that dataset memory can be deallocated after the flann_load_index or flann_build_index calls are completely. This is because the dataset memory is allocated for the arguments of these mex calls and after they are done will most likely be cleaned up by the memory manager after they finish.

I hope all that makes sense. The easiest way that I found to fix this is to modify nearest_neighbors.cpp such that it makes a copy of the dataset passed in via the input arguments to mexFunction. This copy must also be kept track of so that it can be free'd when flann_free_index is called. I have implemented this myself and it seems to resolve all segfaults that I was encountering. There is probably a better fix.

I just wanted to let you all know of the problem. As I said, this bug might be hard to reproduce as it doesn't always cause seg faults. I did not have any problems until I moved to larger datasets.

Just to note I am using

Matlab Version 7.12.0.635 (R2011a) 64-bit (glnxa64)

Thanks,
Dave