tags:

views:

33

answers:

1

I am an Objective C newbie and I'm sure this is an easy question but I can't figure this out: I have a class which declares an instance variable called myDeck, which is part of a custom class I created called Deck. Right now I have this in my code:

In the @interface:

Deck *myDeck;

In my init method:

Deck *ourDeck = [[Deck alloc]init];
myDeck = ourDeck;

So this seems to create the myDeck just fine, and I can stick values in it and run it's methods for a while, but I'm running into a spot where it ceases to exist and I get an EXC_BAD_ACCESS error when trying to use it.

I have tried adding

[myDeck retain];

to no avail, it still fails in the same spots. I don't really know how I should be alloc and initting this, I have a feeling I am missing something, anyone?

+1  A: 

Everything you're doing there seems right; except I think you might have a typo in your first example - the instance variable is called myDeck, not deck, right? If your object is disappearing it's because you called release or autorelease too many times. Memory management in Cocoa is pretty straightforward. Go check out the documentation for all the information you could ever want.

Carl Norum
Oops, yes, that was a typo (in my code the var is named deck and I changed it for clarity in this post).I guess you're right, something must be releasing it too much, I can't figure out what though, I don't call retain or autorelease myself at all, I guess something going on in my program is releasing it. I tried pasting in a couple of dozen [myDeck retain] statements right after the code I posted but that doesn't help heh. I guess it's something that'll seem obvious when I find it.
Cocorico
@Cocorico, are you sure you're keeping good pointers to the object? You can get `EXC_BAD_ACCESS` by trying to access bad pointers, too.
Carl Norum
I don't know, that is probably my problem I am thinking. I don't 100% understand what a good or bad pointer is. Like, in my code, when I use the myDeck object, I am basically just doing stuff like this: [myDeck shuffleDeck]; ... [myDeck printOutCards];If that makes any sense. It's interesting that the printOutCards command I just typed there works in the init method and another method, but not in the method where I am getting errors.
Cocorico
Also, one other thing I wonder: the "myDeck = ourDeck" command I know does not COPY ourDeck into myDeck, I believe it simply makes myDeck a umm.. pointer to ourDeck (if I say that right?). I wonder if that is causing trouble.
Cocorico
Sorry to keep writing but I noticed that my log window keeps talking about GDB mode the last hour or so, which I have not noticed it talking about before. Did I maybe hit some key combo by accident to put me in some different than normal debug mode? I have googled and can't figure out offhand if this is different from just having "debug" as my active build configuration which I think I have had until now. (I just switched it to "release" and the program now works although there is clearly some underlying memory issues I guess right since it fails in debug mode?)
Cocorico