未处理的异常 - 访问冲突读取位置0x00000000

Unhandled exception - Access violation reading location 0x00000000

本文关键字:读取 位置 0x00000000 访问冲突 异常 未处理      更新时间:2023-10-16

我有三个类,一个函数在代码中的某个地方运行良好,如果我把它放在其他地方并且我无法弄清楚它,为什么会发生,就会崩溃。我会很高兴的预先指导。

class BaseClass
{
    friend class B;
protected:
    string m_name;
    BaseClass::BaseClass();                 // implementation doesn't matter
    virtual bool execute  (SRV *p_Srv) = 0;
    virtual void setName(string name)
    {
      m_name = name;
    }
    ~BaseClass(void);               // implementation doesn't matter
};

class derivedClass:public BaseClass
{
    friend class B;
protected:
    derivedClass(void);                 // implementation doesn't matter
    bool execute (SRV *p_Srv);          // implementation doesn't matter
    ~derivedClass(void);                // implementation doesn't matter
};

class B
{
    BaseClasse **array;
    string twoDimArray[2][MAX_PARAMS_SIZE];
    bool function()
    {
     ....
     p_pipeline[i] = new derivedClass(twoDimArray);
     ** EDIT: array[i]->setName("name"); **             <------ problematic line
     p_pipeline[i]->setName("name");                  <------ problematic line
     if (checkIfNewFilterCreated(i, "name") == "-1")                                                    
        throw msg;
     ....
    }
 string B::checkIfNewFilterCreated(int index, string name)
 {
     if (p_pipeline[index] = NULL)
         return "-1";
     else
     {
         m_numOfFiltersCreated++ ;
         return name;   
     }
 }
}

代码使用此命令序列运行良好,但是如果我将"有问题的行"更改为其他地方:

     ....
     p_pipeline[i] = new derivedClass(twoDimArray);
     ** EDIT: array[i] = new derivedClass(twoDimArray); **
     if (checkIfNewFilterCreated(i, "name") == "-1")                                                    
        throw msg;
     p_pipeline[i]->setName("name");                <------ problematic line
     ** EDIT: array[i]->setName("name"); **                <------ problematic line
     ....

,我得到:

访问冲突读取位置0x00000000

如果代码太长,我很抱歉,我挣扎了很长时间......

谢谢。

您在此行中有分配:

if (p_pipeline[index] = NULL)

而不是比较

if (p_pipeline[index] == NULL)

这就是您访问地址0x00000000的原因