Obj-C++ EXC_BAD_ACCESS errors...

Member
Posts: 129
Joined: 2005.02
Post: #1
Could anyone enlighten me as to why I may receive EXC_BAD_ACCESS errors when trying to set member variables of C++ classes? For some reason I cannot access/set any member variables, even if its public/private/protected.

For instance, this will crash:

(in class.h):
public:
bool myBool;

(in class.cpp):
void class::doThis (void)
{
myBool = false; <-- crashes program
}

please someone enlightenme. Also maybe this could be why:
I'm compiling EVERTYHING as Objective-C++.... (but this shouldn't affect anything, as all .cpp files access some ObjC cocoa stuff in one way or another)
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #2
maaaaark Wrote:Could anyone enlighten me as to why I may receive EXC_BAD_ACCESS errors when trying to set member variables of C++ classes? For some reason I cannot access/set any member variables, even if its public/private/protected.

Uh, did you instantiate the class somehow using new? I'm assuming you're using pointers here.

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
you're going to need to show us the object's creation.

remember, C++ objects that are members of ObjC classes must be initialized using placement new in the ObjC class' initializer.
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #4
Why do you use C++? Obj-C is built for classes, which is the only pro I can see to using C++ over C.

It's not magic, it's Ruby.
Quote this message in a reply
Member
Posts: 129
Joined: 2005.02
Post: #5
Thank you for bringing that up. I was new-ing child objects, but not the main parent object. I'll blame it on the other developers at work keeping the lights off today... and... the tunnel vision Wink
Quote this message in a reply
Member
Posts: 129
Joined: 2005.02
Post: #6
Wow that is horrible grammar/spelling. It's been a long day (see above Wink). Why can't you edit replies... only the thread you start? Or am I missing something.
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #7
You most certainly can edit replies - look to the bottom right of your post for the 'edit' button.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Member
Posts: 129
Joined: 2005.02
Post: #8
Maybe it's not in the aqua theme? I'm not seeing it.
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #9
Very strange. It only appears in the very first post. I think I remember this having to do with it only appearing on posts that have titles attached - Carlos?

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
carlos fixed it, but then clearly un-fixed it again :/
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #11
If your post does not have a title, then you cannot edit it in the aqua theme. So if you use the 'quick reply' box at the bottom you cannot edit without changing themes. I feel that the quick reply should have a default title (whatever the thread is) but that's just me.
Quote this message in a reply
Member
Posts: 129
Joined: 2005.02
Post: #12
I totally agree with that. Otherwise, it leaves users like me feeling like idiots...


...or maybe that has something to do with something entirely different Wink
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2008.07
Post: #13
I am having the same problem.

I found someone saying that was an architecture problem (iPhone is not x86, I think it's ARM) and that you should use a diferent type of alignment, like #pragma options align=something...

I tryed putting the members outside and it works (or set the members to static, also works).

Some ideas?
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2011.05
Post: #14
Hi, sorry for digging up this old thread, but I have the exact same problem and maaaaark's solution didn't help (i.e. I didn't understand what to do).

In my current project I have two C++ classes, Game (which handles all the game calculations) and GameGraphics (which does all the rendering). I also have an ObjC++ class GameView (for the OpenGL view).
GameView has this code:
Code:
// GameView.h ...
@interface GameView : NSOpenGLView {
    ...
    Game *game;
    GameGraphics *graphics;
    ...
}
@end

// GameView.mm
- (id)init {
    if((self = [super init])){
        graphics = new GameGraphics();
        ...
    }
    ...
}
If I call a method of GameGraphics that accesses member variables, it gives EXC_BAC_ACCESS unless I add "graphics = new GameGraphics();" before using it. That approach works until a method of the GameGraphics calls another one of its methods.
I also tried using placement new, although I'm not sure whether I did it right (it didn't help).
I've been searching the forum and Google to find a proper way to use C++ classes in ObjC++, but I haven't found anything that solves my problem.

(I guess this is in the wrong subforum, but the thread fits my problem.)
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #15
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.
Quote this message in a reply
Post Reply