iDevGames Forums
Why I recive EXC_BAD_ACCESS - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Why I recive EXC_BAD_ACCESS (/thread-752.html)



Why I recive EXC_BAD_ACCESS - Rincha - Sep 15, 2009 02:45 PM

Hi, in my Class.h I have:
Code:
typedef enum {
    Noise,
    A,
    B,
    C,
    D,
    E,
    F,
    G,
} NoteName;

@interface Class : NSObject {
        ....
    NoteName dominantNote;
        ...

in Class.mm I have:
Code:
@synthesize dominantNote;
       //and work with dominantNote

Now I want to view the value of dominantNote in a UITextField (in viewController.m):
Code:
textFreq.text = (NSString *)class.dominantNote;

but I recive a EXC_BAD_ACCESS in this line, what I'm doing bad?

thanks.


Why I recive EXC_BAD_ACCESS - ThemsAllTook - Sep 15, 2009 03:05 PM

Presumably, either textFreq or class is an uninitialized pointer. EXC_BAD_ACCESS generally means you're trying to read from (?) or write to a portion of memory that's protected from your process, and in my experience is most often caused by trying to dereference an uninitialized/garbaged pointer.


Why I recive EXC_BAD_ACCESS - longjumper - Sep 15, 2009 03:21 PM

Enumerations are integers. In this case, Noise = 0, A = 1, B = 2, etc.

You are trying to cast an enumeration into an NSString * and send it as a parameter to a method that is going to send messages to that NSString.

This makes zero sense.

(I might mention that this would probably be 95% more apparent if you weren't using dot syntax.)


Why I recive EXC_BAD_ACCESS - Josh - Sep 15, 2009 04:33 PM

longjumper Wrote:(I might mention that this would probably be 95% more apparent if you weren't using dot syntax.)
Come on now, don't hate on dot syntax Smile


Why I recive EXC_BAD_ACCESS - Skorche - Sep 15, 2009 04:46 PM

Assuming that you were trying to get the enumeration value name as a string, I'm pretty certain that you can't. Sort of an annoying limitation when trying to debug things.


Why I recive EXC_BAD_ACCESS - longjumper - Sep 16, 2009 06:52 AM

You would be very surprised to see the difference in how fast beginner Objective-C programmers learn memory management based on whether or not they use dot syntax or not.

Dot syntaxers take much, much longer and make routine mistakes. It's fine to use it if you completely understand memory management, rarely use C structs and like to kill baby seals. Otherwise, beginners should stick to setIvar/ivar until they get memory management. Smile


Why I recive EXC_BAD_ACCESS - Josh - Sep 16, 2009 09:41 AM

longjumper Wrote:You would be very surprised to see the difference in how fast beginner Objective-C programmers learn memory management based on whether or not they use dot syntax or not.

Dot syntaxers take much, much longer and make routine mistakes. It's fine to use it if you completely understand memory management, rarely use C structs and like to kill baby seals. Otherwise, beginners should stick to setIvar/ivar until they get memory management. Smile
I have heard that, and it's very odd to me. Personally, I find that dot syntax makes the retain-release cycle much easier to implement properly. But hey, whatever works Smile


Why I recive EXC_BAD_ACCESS - daveh84 - Sep 16, 2009 09:44 AM

Are you saying you don't wrap your retain / release calls in a 2 line method? So for setting a string you might do something like this?

Code:
...
[myClass.name release];
myClass.name = [newName retain];
...

Just curious, I actually have never used the dot syntax stuff in obj-c.


Why I recive EXC_BAD_ACCESS - bmantzey - Sep 16, 2009 11:46 AM

You're actually casting an enum (which is an integer) to a pointer (NSString*). Same amount of data here (4 bytes). You're telling the NSString to look at the address of memory that is equal to the value of the enum (int).


Why I recive EXC_BAD_ACCESS - Rincha - Sep 16, 2009 01:27 PM

Thank for your answers!!

textFreq and my class are initialized. The problem was the casting, so I have used a switch and it works correctly:
Code:
switch (player.dominantNote) {
        case Noise:
            textFreq.text = @"Noise";
            break;
        case A:
            textFreq.text = @"A";
            break;
Ah!! I love dot syntax Wacko
Thanks!!


Why I recive EXC_BAD_ACCESS - longjumper - Sep 16, 2009 01:55 PM

daveh84 Wrote:Are you saying you don't wrap your retain / release calls in a 2 line method? So for setting a string you might do something like this?

Code:
...
[myClass.name release];
myClass.name = [newName retain];
...

Just curious, I actually have never used the dot syntax stuff in obj-c.

Dot notation is syntactic sugar for setIVar: and ivar.
Code:
myObject.name = @"Foo";
is equivalent to
Code:
[myObject setName:@"Foo"];
and
Code:
NSString *foo = myObject.foo;
is equivalent to
Code:
NSString *foo = [myObject foo];

Which means, depending on the implementation of your setter method, myObject.name = @"Foo" could retain, copy or simply assign @"Foo".


Why I recive EXC_BAD_ACCESS - maximile - Sep 16, 2009 01:55 PM

daveh84 Wrote:Are you saying you don't wrap your retain / release calls in a 2 line method? So for setting a string you might do something like this?

Code:
...
[myClass.name release];
myClass.name = [newName retain];
...

Just curious, I actually have never used the dot syntax stuff in obj-c.

When you declare a property, if it's for an Objective-C object you can tell the compiler what the accessors (which it creates) should do (copy, retain, assign). So your code above could just be replaced by myClass.name = newName; and the old value would be automatically released and the new one retained. So I can imagine that catching some people out if they've never had to write accessors by hand.