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

2019-05-25 13:27发布

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条回答
再贱就再见
2楼-- · 2019-05-25 14:17

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; 
}
查看更多
登录 后发表回答