我有一個程序,我只是改變了使用boost :: multi_index_container集合。在完成了這些工作並在調試模式下測試了我的代碼後,我對自己感覺非常好。在發佈模式下增強multi_index_container崩潰
但是,然後我編譯了NDEBUG集的發佈版本,代碼崩潰了。不是馬上,但有時在單線程測試中,通常在多線程測試中。
分段錯誤發生在boost索引更新的內部和旋轉與索引更新相關的函數,並且它們發生的原因是節點具有NULL左和右指針。
我的代碼看起來有點像這樣:
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);
}
}
一旦你把你的時間,想想看這是非常微不足道的。 'replace'只不過是'erase' +'insert'的語法糖,可能會有一些加速,因此迭代器無效(因爲它的指針被刪除)。用map('ordered_unique')這樣做會產生相同的行爲。遺憾的是,圖書館沒有提供「檢查迭代器」,它會在你的調試時間內得到方便! – 2010-05-20 06:45:24
@Matthieu:是的,擦除並插入。這很明顯,當你考慮它。但是,因爲我正在替換以前使用STL映射的代碼,並直接修改了關鍵衝突的映射值,所以我的大腦完全通過了這個問題。 – 2010-05-20 19:16:47