| 1 | // | 
|---|
| 2 | // Boost.Geometry rtree failure? | 
|---|
| 3 | // | 
|---|
| 4 | // Scenario: | 
|---|
| 5 | // * Add some values to a rtree | 
|---|
| 6 | // * Remove (some or all of) them in different order | 
|---|
| 7 | // * Add another element -> BOOM | 
|---|
| 8 | // | 
|---|
| 9 | // Compile: | 
|---|
| 10 | // clang++ rtree-crash.cpp -std=c++11 -Wall -I /opt/local/include/ -o rtree-crash | 
|---|
| 11 | // | 
|---|
| 12 | // FAILS: | 
|---|
| 13 | // rtree-crash 14 5 | 
|---|
| 14 | // rtree-crash 30 15 | 
|---|
| 15 | // | 
|---|
| 16 | // PASS: | 
|---|
| 17 | // rtree-crash <n> 1 | 
|---|
| 18 | // | 
|---|
| 19 | // SEGFAULT: | 
|---|
| 20 | // rtree-crash 1000 123 (with -DNDEBUG) | 
|---|
| 21 | // | 
|---|
| 22 | // * Also fails in two dimensions e.g. with value_type{i, i + 1} | 
|---|
| 23 | // | 
|---|
| 24 |  | 
|---|
| 25 | #include <boost/geometry.hpp> | 
|---|
| 26 | #include <boost/geometry/index/rtree.hpp> | 
|---|
| 27 | #include <boost/lexical_cast.hpp> | 
|---|
| 28 |  | 
|---|
| 29 | #include <iostream> | 
|---|
| 30 |  | 
|---|
| 31 | #include <cassert> | 
|---|
| 32 |  | 
|---|
| 33 | typedef boost::geometry::model::point< | 
|---|
| 34 | int, 1, boost::geometry::cs::cartesian> value_type; | 
|---|
| 35 | typedef boost::geometry::index::quadratic<4> strategy; | 
|---|
| 36 | typedef boost::geometry::index::rtree<value_type, strategy> tree; | 
|---|
| 37 |  | 
|---|
| 38 | // Add n values | 
|---|
| 39 | void add_values(tree& tr, const long n) { | 
|---|
| 40 | for (long i = 0; i < n; ++i) { | 
|---|
| 41 | std::cout << "insert " << i << std::endl; | 
|---|
| 42 | tr.insert(value_type{i}); | 
|---|
| 43 | } | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | // Remove n values, order determined by m. | 
|---|
| 47 | // If gcd(m, n) == 1, all elements are removed. | 
|---|
| 48 | void remove_values(tree& tr, const long n, const long m) { | 
|---|
| 49 | for (long j = 0; j < n; ++j) { | 
|---|
| 50 | long const i = (j * m) % n; | 
|---|
| 51 | std::cout << "remove " << i << std::endl; | 
|---|
| 52 | tr.remove(value_type{i}); | 
|---|
| 53 | } | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | int main(int const argc, char const** const argv) { | 
|---|
| 57 | assert(3 == argc); | 
|---|
| 58 |  | 
|---|
| 59 | long const n = boost::lexical_cast<long>(argv[1]); | 
|---|
| 60 | long const m = boost::lexical_cast<long>(argv[2]); | 
|---|
| 61 |  | 
|---|
| 62 | tree tr; | 
|---|
| 63 | add_values(tr, n   ); | 
|---|
| 64 | remove_values(tr, n, m); | 
|---|
| 65 | add_values(tr, 1   ); | 
|---|
| 66 | } | 
|---|