Unhandled exception - Access violation reading location 0x00000000

-1

I have three classes an one function that runs fine on certain place in the code and crashes if I put it on other place and I can't figure it out, why it happens. I will be happy fore guidance.

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;   
     }
 }
}

The code runs fine with this sequence of command, but if I change the 'problematic line' to other place:

     ....
     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
     ....

, I get:

Access violation reading location 0x00000000

I am sorry if the code too long, I struggle with it for a long time...

Thanks.

c++
polymorphism
asked on Stack Overflow Jan 8, 2015 by user1673206 • edited Aug 31, 2018 by Guy Avraham

1 Answer

3

You have assignment in this line:

if (p_pipeline[index] = NULL)

instead of comparisson

if (p_pipeline[index] == NULL)

That's why you are accessing address 0x00000000

answered on Stack Overflow Jan 8, 2015 by Kuba

User contributions licensed under CC BY-SA 3.0