Qt read acces violation at:0x0, flags=0x0 (first c

2019-05-25 13:56发布

问题:

I have a very simple piece of code as follows:

Letter* Vakje::geefLetter() const
{
    return m_letter;
}

The class Vakje gets initialized with a NULL-pointer for m_letter, but I'm still getting a read access violation error, could someone please help?

this is the code that calls the function:

bool Spelbord::positionIsEmpty(int rij, int kolom) { 
    if (vakjes[rij][kolom]->geefLetter()==nullptr) return true;
    else return false; 
}

回答1:

If vakjes[rij][kolom] is NULL, then trying to dereference that pointer -> results in undefined behavior (usually: crash).

Just change the code to:

bool Spelbord::positionIsEmpty(int rij, int kolom) { 
  if (vakjes[rij][kolom] == nullptr) return true;
  if (vakjes[rij][kolom]->geefLetter()==nullptr) return true; 

  return false; 
}