http://blog.163.com/cp7618@yeah/blog/static/702347772012114103640735/
问题描述
游服文件TGameObjmap.h类TGameObjMap封装使用std::map, 在使用过程中会出现crash.
原因分析
我们发现在使用TGameObjMap中,由于多线程没同步好或在使用迭代器同时内存改变,才导致的crash.
我们定位到crash时的代码:
void _Inc()
{ // move to node with next larger value
#if _HAS_ITERATOR_DEBUGGING
if (this->_Mycont == 0
|| _Ptr == 0
|| _Isnil(_Ptr))
{
_DEBUG_ERROR("map/set iterator not incrementable");
_SCL_SECURE_OUT_OF_RANGE;
}
#else
_SCL_SECURE_VALIDATE(this->_Has_container());
if (_Isnil(_Ptr))---------------------------------------->Why crash here?
{
_SCL_SECURE_OUT_OF_RANGE;
// end() shouldn't be incremented, don't move if _SCL_SECURE is not turned on
}
#endif /* _HAS_ITERATOR_DEBUGGING */
else if (!_Isnil(_Right(_Ptr)))
_Ptr = _Min(_Right(_Ptr)); // ==> smallest of right subtree
else
{ // climb looking for right subtree
_Nodeptr _Pnode;
while (!_Isnil(_Pnode = _Parent(_Ptr))
&& _Ptr == _Right(_Pnode))
_Ptr = _Pnode; // ==> parent while right subtree
_Ptr = _Pnode; // ==> parent (head if end())
}
}
这是std::map中C++红黑树迭代器的标准实现。
我们复习下有关红黑树知识
红黑树定义
红黑树是一种自平衡二叉查找树,是在计算机科学中用到的一种数据结构,典型的用途是实现关联数组,如STL map 和set,和普通二叉树相比它的实现上稍微有些复杂,但它的操作有着良好的最坏情况运行时间,并且在实践中是高效的: 它可以在O(log n)时间内做查找,插入和删除,这里的n是树中元素的数目。
红黑树性质
红黑树是每个节点都带有颜色属性的二叉查找树,颜色为红色或黑色。在二叉查找树强制一般要求以外,对于任何有效的红黑树我们增加了如下的额外要求:
性质1. 节点是红色或黑色。
性质2. 根是黑色。
性质3. 所有叶子都是黑色(叶子是NIL节点)。
性质4. 每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点)
性质5. 从任一节点到其每个叶子的所有简单路径都包含相同数目的黑色节点。
算法导论原版定义
A red-black tree is a binary search tree with one extra bit of storage per node: its color, which can be either RED or BLACK. By constraining the node colors on any simple path from the root to a leaf, red-black trees ensure that no such path is more than twice as long as any other, so that the tree is approximately balanced. Each node of the tree now contains the attributes color, key, left, right, and p. If a child or the parent of a node does not exist, the corresponding pointer attribute of the node contains the value NIL. We shall regard these NILs as being pointers to leaves (external nodes) of the binary search tree and the normal, key-bearing nodes as being internal nodes of the tree. A red-black tree is a binary tree that satisfies the following red-black properties:
1. Every node is either red or black.
2. The root is black.
3. Every leaf (NIL) is black.
4. If a node is red, then both its children are black.
5. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
典型的红黑树如下图所示
红黑树的典型操作
插入/删除/左旋/右旋/查找/遍历
我们现在再回过头来分析下std::map中的_Inc()代码,当出现迭代器内存改变时,this->_Mycont== 0 || _Ptr== 0 || _Isnil(_Ptr) 就会成true.这时在DEBUG下面会调用
assert进行提示,release会调用InvalidParameterHandler进行参数无效提示,故调用_Inc()就出错。
我们再定位到_inc()的上一级代码调用:
const_iterator& operator++()
{ // preincrement
_Inc();
return (*this);
}
这时++就出错了,程序系统就会crash 。
解决方案
1)不能在多线程中应用TGameObjMap,TGameObjMap(std::map)是不支持多线程应用。
2)在TGameObjMap的迭代器时,要防止操作让内存改变,或在进行迭代器使用时,先对上面的内存参数先进行校验,以安全使用,不会出现crash.
案例推广