Why I recive EXC_BAD_ACCESS
Hi, in my Class.h I have:
in Class.mm I have:
Now I want to view the value of dominantNote in a UITextField (in viewController.m):
but I recive a EXC_BAD_ACCESS in this line, what I'm doing bad?
thanks.
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.
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.
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.)
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.)
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

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.
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.
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.

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.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
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.

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?
Just curious, I actually have never used the dot syntax stuff in obj-c.
Code:
...
[myClass.name release];
myClass.name = [newName retain];
...
Just curious, I actually have never used the dot syntax stuff in obj-c.
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).
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:
Ah!! I love dot syntax
Thanks!!
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;

Thanks!!
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";
Code:
[myObject setName:@"Foo"];
Code:
NSString *foo = myObject.foo;
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".
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.
Possibly Related Threads...
Thread: | Author | Replies: | Views: | Last Post | |
Thread 4: EXC_BAD_ACCESS | cmhubert | 2 | 4,789 |
Sep 29, 2013 07:26 AM Last Post: mawigator |
|
EXC_BAD_ACCESS | Holyhoppsan | 2 | 8,503 |
Aug 10, 2010 01:44 PM Last Post: Holyhoppsan |
|
EXC_BAD_ACCESS in objc_msgSend | saltwater | 3 | 7,307 |
Sep 7, 2009 01:24 PM Last Post: saltwater |
|
Random EXC_BAD_ACCESS | bmpix | 2 | 4,675 |
Feb 17, 2009 10:54 PM Last Post: smallstepforman |
|
EXC_BAD_ACCESS Sound error | wonza | 5 | 7,188 |
Jan 26, 2009 03:35 AM Last Post: wonza |