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;
}
Related videos on Youtube
Author by
Cédric Vandelaer
I study computer science at the UHasselt university in Belgium.
Updated on June 04, 2022Comments
-
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 almost 11 yearsExactly 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 almost 11 yearsbut I'm doing a check to see if it's NULL :S
-
R. Martinho Fernandes almost 11 yearsI 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 almost 11 yearsWhich code shows the read access violation error? You are not showing client code.
-
Prasanth Kumar almost 11 yearsThere is nothing here. You can't talk about checks and violations without showing them.
-
Bart almost 11 yearsShow us the surrounding code. I.e, what's calling it and what's using the result.
-
Frank Osterfeld almost 11 yearsThe containing Vakje object might be invalid/accessed via dangling pointer. Try with a debugger/and or Valgrind (if on Linux/OS X).
-
Mat almost 11 yearsAre you calling
geefLetter
on a null Vakje? -
Cédric Vandelaer almost 11 yearsthis is the code: bool Spelbord::positionIsEmpty(int rij, int kolom) { if (vakjes[rij][kolom]->geefLetter()==nullptr) return true; else return false; }
-
Bart almost 11 yearsEdit 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 almost 11 years@Cédric Please ask a complete question, editing it to include all the relevant code.
-
Prasanth Kumar almost 11 yearsI 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 ofrij
andkolom
out of range. The question is not if I am going to crash, but when". -
Cédric Vandelaer almost 11 yearsthe rij and kolom values can't go out of range :S
-
Bart almost 11 yearsSo 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 almost 11 yearsVakje was null, sorry for the stupid question, thanks :)
-
Prasanth Kumar almost 11 years@Cédric Out of range, null element, value not initialized... whatever.
-