0

I have a variable int _width; being set in a constructor of a class Stage as follows:

Stage::Stage():_width(33), _height(23)
{
...
}

And later I am using it in a pathfinding method, where I call:

ex = fminf(_width - 1, current->x + 1);

which is just setting an end X point for something else. The problem is I get a Thread 1: EXC_BAD_ACCESS (code=1, address=0x8) error. When I try to log it out with p _width I get:

(lldb) p _width
error: Couldn't apply expression side effects : Couldn't dematerialize a result variable: couldn't read its memory

I'm not sure what's going on here. I'm not resetting the variable anywhere.

Evan Ward
  • 1,371
  • 2
  • 11
  • 23
  • 3
    That suggests the `this` pointer is null, i.e. you've invoked a member function on an object which doesn't exist. – Jonathan Wakely Dec 10 '14 at 18:32
  • As in the `Stage` is getting deallocated? – Evan Ward Dec 10 '14 at 18:33
  • no, as in it was never allocated in the first place (deallocating doesn't set pointers to null, it just makes the memory they point to no longer part of your program) – Jonathan Wakely Dec 10 '14 at 18:34
  • As in: `Stage* s = nullptr; s->foo();` That will compile, but has undefined behaviour. Typically inside the `foo()` member function the `this` pointer would be null, and trying to access any member variables of the object will cause the kind of access error you are seeing. – Jonathan Wakely Dec 10 '14 at 18:35
  • i'm creating the stage with `currentStage(new Stage())` right now. and just calling `currentStage->update(delta)` later in a loop. I'm not sure why this is behaving this way since there was no problem until i introduced the width variable, other variables were behaving correctly. – Evan Ward Dec 10 '14 at 18:38
  • What is `currentStage`? A member of another object? – Jonathan Wakely Dec 10 '14 at 18:40
  • Yes, controlled by an `Engine` class right now, that updates everything, including the `Stage`. `currentStage` is just the currently loaded `Stage` to update. – Evan Ward Dec 10 '14 at 18:41
  • I've figured out the problem, in the constructor of `Stage` i was calling a function that referred back to stage, which wasn't finished being constructed. Thank you for you help. – Evan Ward Dec 10 '14 at 18:44
  • nit-pick that has nothing to do with your problem, you shouldn't start member names with _, those are technically reserved for variables that are only alive in the file (but I avoid them all together). – IdeaHat Dec 10 '14 at 19:06
  • @IdeaHat - you mean private? it is private if that's what you mean. – Evan Ward Dec 10 '14 at 20:02
  • @EvanWard http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier sorry I got it wrong, "Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace" – IdeaHat Dec 10 '14 at 20:04
  • Ah, yeah i'm not too proficient with C++, i'm used to _name being a declaration that the member is private/protected. – Evan Ward Dec 10 '14 at 20:10

0 Answers0