我写了一个std :: map的比较函数,所以我可以有自定义的键类型。std :: map比较函数和NULL
std::map<GGString *, GGObject *, GGDictionaryMapCompare> _map;
...,增加了元素
class GGDictionaryMapCompare
{
public:
bool operator()(GGString * lhs, GGString * rhs)
{
return strcmp(lhs->str(), rhs->str()) < 0;
}
};
代码:是导致飞机失事的原因
GGObject *GGDictionary::addKeyObject(GGString *theKey, GGObject *theObject)
{
if (theKey == NULL || theObject == NULL)
return NULL;
_map.insert(std::pair<GGString *, GGObject *>(theKey, theObject));
return theObject;
}
代码:
GGObject *GGDictionary::objectForKey(GGString *theKey)
{
if (theKey == NULL)
return NULL;
std::map<GGString *, GGObject *, GGDictionaryMapCompare>::iterator ii = _map.find(theKey);
if (ii == _map.end())
return NULL;
return GGAutoRelease(ii->second);
}
堆栈跟踪:
#0 0x00009f15 in GGString::str()
#1 0x0004a4c4 in GGDictionaryMapCompare::operator()(GGString*, GGString*)
#2 0x0004a3d3 in std::_Rb_tree<GGString*, std::pair<GGString* const, GGObject*>, std::_Select1st<std::pair<GGString* const, GGObject*> >, GGDictionaryMapCompare, std::allocator<std::pair<GGString* const, GGObject*> > >::find(GGString* const&)
#3 0x00049b04 in std::map<GGString*, GGObject*, GGDictionaryMapCompare, std::allocator<std::pair<GGString* const, GGObject*> > >::find(GGString* const&)
#4 0x00048ec9 in GGDictionary::objectForKey(GGString*)
问题是lhs进来NULL。我从不在地图中插入NULL,所以这不应该发生。任何想法为什么?或者我只是在做比较功能错误?我可以防止得到NULL,但它似乎有什么错,我不想治愈症状,而不是问题。
感谢
显示你在哪里添加元素。 – imreal
你能发表一些关于你如何使用它的代码,和/或你如何向地图添加元素?我可以尝试重现这个问题。我没有看到任何与该代码有关的明显问题,因此在实现中可能会有些奇怪。 –
如果你的GGString进入'NULL',它将插入你正在寻找的'NULL' *键*,而不是'NULL' *值* –