我遇到了一个由 CSceneNode* pRoot = nodes[0]; 引起的随机崩溃的问题;//真正的问题是 = nodes[0]; 崩溃消息是:“0x0059d383”处的指令引用了“0x00000000”处的内存。无法“读取”内存。 我没有看到问题,请帮帮我好吗?
在保存.cpp
void CNESave::SaveLocation(CNELocation* pLoc)
// Other code
CSceneNode* scene = pLoc->GetScene();
vector<CSceneNode*> nodes;
scene->GetNodes(GetNodesByPartOfName, nodes, &string("_Ldynamic"));
CSceneNode* pRoot = nodes[0]; //This is the problem causing random crashes!
// If I change it (just for testing) to CSceneNode* pRoot = scene
// them it isn't crashing.
// So CSceneNode* pRoot is OK and the problematic part is = nodes[0];
在场景节点.cpp中
bool GetNodesByPartOfName(CSceneNode* node, const void *data)
{
string *str = (string *)data;
return node->GetName().find(*str)!=npos;
}
void CSceneNode::GetNodes( GetNodesFunc gf, vector<CSceneNode*> &nodes,
const void *data)
{
if (gf(this, data)) nodes.push_back(this);
SceneNodeIterator begin=FirstChild(), end=LastChild();
for (;begin!=end;begin++) ((CSceneNode*)*begin)->GetNodes(gf, nodes, data);
}
CSceneNode* CSceneNode::CreateNew()
{
return new CSceneNode();
}
// a lot of other code
在 SceneNode.h 中
class EXPORTDECL CSceneNode;
typedef bool (*GetNodesFunc)(CSceneNode *node, const void *data);
EXPORTDECL bool GetNodesByPartOfName(CSceneNode* node, const void *data);
typedef vector<CSceneNode*>::iterator SceneNodeIterator;
class EXPORTDECL CSceneNode : public CTreeNode<CSceneNode, true>
{
public:
//construction & destruction
CSceneNode();
virtual ~CSceneNode();
virtual CSceneNode *CreateNew();
// a lot of other code
已解决。非常感谢你们。
最佳答案
如果 node->GetName()
的结果不包含字符串 _Ldynamic
,则会崩溃。
因为在这种情况下 GetNodesByPartOfName(..)
将返回 false 而 GetNodes(..)
将不会执行 nodes.push_back(this)
将留下一个空 vector ,稍后将在从 GetNodes(..)
返回后尝试访问该空 vector 的第一个元素。
关于c++ - 内存不能是 "read"。 - 随机崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3839199/