QT作为界面,算法用C++实现,软件启动在Release下不报错,但是在Debug下提示错误:Expression : map / set iterator not dereferencable
并且不是每次调试都会出错,调试根本无法定位到具体出错点。
于是采用了笨办法,从界面开始逐步屏蔽代码,一段段排查,最后定位到了一个出错的函数:
void PainterObjectCache::Add(const Symbol* symbol, PainterObject* obj, int group)
{
SymbolPainterObjectMap::iterator it = gdi_object_map.find(symbol);
if (it == gdi_object_map.end()) {
gdi_object_map[symbol] = std::vector<PainterObject*>();//这句导致的
it = gdi_object_map.begin();
}
if (it == gdi_object_map.end()) {
return;
}
int level_count = int(it->second.size()) / PAINTER_OBJ_INDEX_MAX;
if (level_count <= group)
it->second.resize((group+1)*PAINTER_OBJ_INDEX_MAX, nullptr);
it->second[PAINTER_OBJ_INDEX_MAX*group+obj->GetObjectType()] = obj;
}
上述的病句改为下面的,就没再出错。
void PainterObjectCache::Add(const Symbol* symbol, PainterObject* obj, int group)
{
SymbolPainterObjectMap::iterator it = gdi_object_map.find(symbol);
if (it == gdi_object_map.end()) {
//gdi_object_map[symbol] = std::vector<PainterObject*>();改为下面语句。该语句会引起配准软件在调试模式下启动时偶尔出现崩溃“Expression : map / set iterator not dereferencable”
gdi_object_map.insert(std::pair<const Symbol*, std::vector<PainterObject*> >(symbol, std::vector<PainterObject*>()));
it = gdi_object_map.begin();
}
if (it == gdi_object_map.end()) {
return;
}
int level_count = int(it->second.size()) / PAINTER_OBJ_INDEX_MAX;
if (level_count <= group)
it->second.resize((group+1)*PAINTER_OBJ_INDEX_MAX, nullptr);
it->second[PAINTER_OBJ_INDEX_MAX*group+obj->GetObjectType()] = obj;
}
这俩的不同,可以归结为:map 赋值方法[]和insert的区别。按照往常的理解
下标[]的方式插入,如果原本key不存在则会先创建对应的记录,然后再进行赋值;
insert
方式插入,如果key不存在,则插入记录,如果存在则什么都不做。
但是在矢量vector作为map的value的时候,猜测可能显式赋值的vector和默认赋值的不是同一个。毕竟我们使用习惯是,不管对应的key是否存在,直接用[]来进行赋值。
等具体研究后再补充。先做个记录。
版权声明:本文为hanbing6174原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。