Boost multi_index_container crash in release mode
- by Zan Lynx
I have a program that I just changed to using a boost::multi_index_container collection. After I did that and tested my code in debug mode, I was feeling pretty good about myself.
However, then I compiled a release build with NDEBUG set, and the code crashed. Not immediately, but sometimes in single-threaded tests and often in multi-threaded tests.
The segmentation faults happen deep inside boost insert and rotate functions related to the index updates and they are happening because a node has NULL left and right pointers.
My code looks a bit like this:
struct Implementation {
typedef std::pair<uint32_t, uint32_t> update_pair_type;
struct watch {};
struct update {};
typedef boost::multi_index_container<
update_pair_type,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<
boost::multi_index::tag<watch>,
boost::multi_index::member<update_pair_type, uint32_t, &update_pair_type::first>
>,
boost::multi_index::ordered_non_unique<
boost::multi_index::tag<update>,
boost::multi_index::member<update_pair_type, uint32_t, &update_pair_type::second>
>
>
> update_map_type;
typedef std::vector< update_pair_type > update_list_type;
update_map_type update_map;
update_map_type::iterator update_hint;
void register_update(uint32_t watch, uint32_t update);
void do_updates(uint32_t start, uint32_t end);
};
void Implementation::register_update(uint32_t watch, uint32_t update)
{
update_pair_type new_pair( watch_offset, update_offset );
update_hint = update_map.insert(update_hint, new_pair);
if( update_hint->second != update_offset ) {
bool replaced _unused_ = update_map.replace(update_hint, new_pair);
assert(replaced);
}
}