views:

972

answers:

2

I'm a veteran .NET developer making my first foray into Objective C programming. I'm having difficulty with a property of an enum type. Some context... I have an class header and enum like this:

typedef enum  {
    Open,
    Unavailable,
    Unknown
} LocationStatus;

@interface Location : NSObject {

    LocationStatus status;
}

@property (nonatomic) LocationStatus status;

@end

and an implementation that looks like this:

@implementation Location

@synthesize status;

@end

At some point in the code, I'm setting the value like this:

location1.status = Open;

The debugger then evaluates this as having the correct value, and it is resolving to the correct enum (note also that there are other properties not shown here... they too evaluate properly).

Later on in the code, I attempt to read that property like this:

LocationStatus status = location.status;

At this point in the code, the debugger is able to evaluate all the properties of my class correctly, except Status, which shows a memory address, but not an actual value. When the execution reaches this line, I consistently get a EXC_BAD_ACCESS error in the console, and the app crashes.

I'm pretty sure this reflects a fundamental misunderstanding on my part on how to use properties and enums in Objective C (and probably C in general). If anyone could shed some light on this, I'd be most grateful.

+3  A: 

EXC_BAD_ACCESS almost always means you're trying to use a reference to an object that's been deallocated (usually an over-release bug). Search for that error here on SO to find lots of advice on tracking it down.

Sixten Otto
That was my first thought... that I'd released the value somewhere. What's confusing me is that a search of the code shows that there is only one place in the entire codebase that this property gets a value assigned to it, and putting a breakpoint there proves that this assignment is happening only once. In addition, because LocationStatus is an enum, I don't have to allocate it, which means I can't specify how it's released (I'm totally open to being wrong here, mind you). So how could it have been released early and how can I prevent it? Is there some better way of assigning enum values?
damion
My first thought would be that the thing that's expired is the contents of your location variable in that assignment. But turn on zombies, and you can see for sure!
Sixten Otto
A: 

It might be to late to answer this but I did notice one thing in your code. You are using 2 different variables in you code location1 and location (without the 1).

EXEC_BAD_ACCESS generally means that you are trying to send a message to an object that does not exist. Usually this is because it has been deallocated. However, in your case it appears that it never existed in the first place.

As you noted you don't allocate an enum. But its not the enum that is the problem. The "dot" syntax in objective-c is just a short cut for sending an accessor message.

Your code is equivalent to LocationStatus status = [location status] which sends the synthasized -(LocationStatus)status{} message to the non-existent location object (unless of course location1 was just a typo in you post, but not in your code, which makes my comment irrelevant). So just change location.status to location1.status and you should be good to go (unless, of course, location1 is being released before you send the message to it).

sc0rp10n