Obj-C++ EXC_BAD_ACCESS errors...

Apprentice
Posts: 5
Joined: 2011.05
Post: #16
(May 23, 2011 08:23 AM)OneSadCookie Wrote:  NSOpenGLView is not initialized by -init. I don't believe your code will be being called at all. Depending on how you have this set up, you'll either be being initialized by -initWithCoder: or by -initWithFrame:; once you figure out which, you'll know where to initialize graphics.
I know, in IB I have a Custom View with the class GameView (the code is from this example: http://developer.apple.com/library/mac/#...TS10004501). That setup has worked fine so far.
The problem is that I can do whatever I want with graphics until it tries to access any of its member variables.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #17
And that's because your "graphics = new GameGraphics();" line is not being run because -init is not being called, so "graphics" is still NULL. Once you move it to the correct initializer, everything will be fine.

If you don't believe me, put an assert(graphics) before you call graphics->someMethod() Rasp
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2011.05
Post: #18
Interesting, I seem to remember having tested whether -init was called, but you're right!
I put it in -initWithFrame now as you suggested, and it seems to work there. But when I call "graphics->setWidth( ... );" in another method (-reshape), it gives me EXC_BAD_ACCESS on the line "width = newWidth;" (width being the member variable of the class and newWidth the passed width).
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #19
I still recommend the assertion to help diagnose the new problem.

If that doesn't help, and your intuition isn't telling you anything, Guard Malloc or valgrind are good tools.
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2011.05
Post: #20
Well I've put
Code:
if(graphics == NULL)
        graphics = new GameGraphics(viewWidth, viewHeight);
into every method that uses the graphics object and that seems to work, but i don't understand why I have to reinitialize it everywhere. Shouldn't it get "saved" once I stick it into graphics?

And thanks for helping me even though you hate C++ Smile
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #21
If you
a) are crashing due to graphics == NULL in a method, and
b) are initializing graphics in your designated initializer
then
at least one instance of this class is being created without going through that initializer
which leads to two interesting questions,
a) how many instances of this class are there?
b) which initializer are they using?

The former is straightforward to answer; NSLog(@"%p", self); in the places you've had to check for NULL. It sounds like you think you have a single instance, so if you see multiple different values printed, that's a huge red flag.

The latter is less straightforward to answer. Since you're an NSOpenGLView subclass, overriding -init, -initWithFrame:, -initWithCoder: and -initWithFrame:pixelFormat: and logging in each ought to do the trick, but it's not entirely inconceivable that you might be being initialized in some other way (or even, if you've written wacky code, not being initialized at all).
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2011.05
Post: #22
It turns out that in IB the NSView (in which my GameView is) also had the class GameView and didn't use -initWithFrame. Guess I wasn't paying attention when I set that up.
Thanks for the great help!!
Quote this message in a reply
Post Reply