Why I recive EXC_BAD_ACCESS

Nibbie
Posts: 4
Joined: 2009.09
Post: #1
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.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
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.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #3
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.)
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #4
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
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #5
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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #6
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
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #7
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
Quote this message in a reply
Member
Posts: 35
Joined: 2009.01
Post: #8
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.
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #9
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).
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2009.09
Post: #10
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!!
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #11
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".
Quote this message in a reply
Member
Posts: 283
Joined: 2006.05
Post: #12
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Thread 4: EXC_BAD_ACCESS cmhubert 2 2,122 Sep 29, 2013 07:26 AM
Last Post: mawigator
  EXC_BAD_ACCESS Holyhoppsan 2 4,896 Aug 10, 2010 01:44 PM
Last Post: Holyhoppsan
  EXC_BAD_ACCESS in objc_msgSend saltwater 3 4,957 Sep 7, 2009 01:24 PM
Last Post: saltwater
  Random EXC_BAD_ACCESS bmpix 2 3,108 Feb 17, 2009 10:54 PM
Last Post: smallstepforman
  EXC_BAD_ACCESS Sound error wonza 5 4,590 Jan 26, 2009 03:35 AM
Last Post: wonza