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

10,420

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; 
}
Share:
10,420

Related videos on Youtube

Cédric Vandelaer
Author by

Cédric Vandelaer

I study computer science at the UHasselt university in Belgium.

Updated on June 04, 2022

Comments

  • Cédric Vandelaer
    Cédric Vandelaer about 2 years

    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; 
    }
    
    • Mats Petersson
      Mats Petersson almost 11 years
      Exactly what else do you expect? Accessing a pointer that is NULL is undefined behaviour, which is most cases means "crash the program" [not ALWAYS the case for UB, but in this sort of case, quite typical].
    • Cédric Vandelaer
      Cédric Vandelaer almost 11 years
      but I'm doing a check to see if it's NULL :S
    • R. Martinho Fernandes
      R. Martinho Fernandes almost 11 years
      I see no check. Were we supposed to guess that some of your code somewhere has a null-check? What else are we supposed to guess?
    • Prasanth Kumar
      Prasanth Kumar almost 11 years
      Which code shows the read access violation error? You are not showing client code.
    • Prasanth Kumar
      Prasanth Kumar almost 11 years
      There is nothing here. You can't talk about checks and violations without showing them.
    • Bart
      Bart almost 11 years
      Show us the surrounding code. I.e, what's calling it and what's using the result.
    • Frank Osterfeld
      Frank Osterfeld almost 11 years
      The containing Vakje object might be invalid/accessed via dangling pointer. Try with a debugger/and or Valgrind (if on Linux/OS X).
    • Mat
      Mat almost 11 years
      Are you calling geefLetter on a null Vakje?
    • Cédric Vandelaer
      Cédric Vandelaer almost 11 years
      this is the code: bool Spelbord::positionIsEmpty(int rij, int kolom) { if (vakjes[rij][kolom]->geefLetter()==nullptr) return true; else return false; }
    • Bart
      Bart almost 11 years
      Edit your question to contain the relevant code please. Don't hide it within comments. And are you sure that the "table" has non-null Vakje-s?
    • Prasanth Kumar
      Prasanth Kumar almost 11 years
      @Cédric Please ask a complete question, editing it to include all the relevant code.
    • Prasanth Kumar
      Prasanth Kumar almost 11 years
      I read this line if (vakjes[rij][kolom]->geefLetter()==nullptr)... as "Hello, I am a native 2D array and I am going to crash because you are going to call me with values of rij and kolom out of range. The question is not if I am going to crash, but when".
    • Cédric Vandelaer
      Cédric Vandelaer almost 11 years
      the rij and kolom values can't go out of range :S
    • Bart
      Bart almost 11 years
      So answer this: what are the dimensions? When it crashes, what does it crash on (the indices)? Might it be that the Vakje is null? Did you check that? Fire up a debugger and let us know. (OT: might I advice you to start programming in English and not Dutch. You'll thank me in the future).
    • Cédric Vandelaer
      Cédric Vandelaer almost 11 years
      Vakje was null, sorry for the stupid question, thanks :)
    • Prasanth Kumar
      Prasanth Kumar almost 11 years
      @Cédric Out of range, null element, value not initialized... whatever.