flann-lib / flann

Fast Library for Approximate Nearest Neighbors
http://people.cs.ubc.ca/~mariusm/flann
Other
2.23k stars 650 forks source link

Inexplicable run-time error when combined with PCL #347

Open KenYN opened 7 years ago

KenYN commented 7 years ago

I was having a mysterious runtime error in the PCL 1.7 library on Ubuntu 14.04 when compiling with -std=c++11 (or perhaps another flag) - I was creating a point cloud and got a runtime error with what appeared to be an empty virtual table on an object of type flann::base_any_policy. Much debugging and hacking about later, I found that in kdtree_single_index.h if I changed:

(*this)["algorithm"] = FLANN_INDEX_KDTREE_SINGLE;

To:

(*this)["algorithm"] = any(FLANN_INDEX_KDTREE_SINGLE);

The program runs successfully.

There might have been some incompatibility between g++ versions used, or new template resolution rules for C++11 (a colleague's Ubuntu 16.04 crashed similarly), but I think the change above should be backward-compatible.

MattC11 commented 7 years ago

I had the same problem with PCL 1.8, MacOS and Clang 7.3.1.

I thought it was variable naming but my issue turned out to be using Flann in both a delayed load library and in my main application. The policy variable in any.h was being shared between the two and that caused problems.

As a solution I applied a patch to make the function getting the variable static so that it wouldn't be share:

--- a/src/cpp/flann/util/any.h +++ b/src/cpp/flann/util/any_fix.h @@ -138,7 +138,7 @@ SMALL_POLICY(bool);

/// This function will return a different policy for each type. template -base_any_policy get_policy() +static base_any_policy get_policy() { static typename choose_policy::type policy; return &policy;

victl commented 5 years ago

Thank you @MattC11 ! Your solution is perfect.

I'm using FLANN 1.9.1 on macOS Mojave with PCL 1.9.1. Compiler is Clang 10.0.0

Can't believe this bug is still there for such a long time.