c++ - 内存不能是 "read"。 - 随机崩溃

标签 c++ windows

我遇到了一个由 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/

相关文章:

c - Linux native 支持 ansicon 导入的转义序列吗?

c++ - 在构造函数中初始化私有(private) std::array 成员

c++ 指向 int 指针的指针

c++ - 默认退出函数实现

Php exec() 与 mysql 导入不起作用

java - 在 Windows 中为 JAVA_HOME 更新系统变量

c++ - 在 C++ 中将值与自然数进行比较

c++ - 如何使用 libjson 创建数组?

c++ - 是什么导致 WriteFile 返回 ERROR_ACCESS_DENIED?

c++ - 在 VS2008 中为 WinCE 构建 OpenSSL