0

i get EXC_BAD_ACCESS while formatting string.

NSString *object = [[NSUserDefaults standardUserDefaults] stringForKey:@"ObjectNumber"];
NSString *pin = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithFormat:@"Pin%i ",object.intValue]];
NSString *msg = [NSString stringWithFormat:@"%@N", pin];

NSString *msg2 = @"0001N";

NSLog(@"Sending %@", msg);

tcpConnection *Obj = [tcpConnection alloc];
[Obj loadUpConnection:msg];

If i use msg2 everything works fine. But if i use msg it gets exc_bad_access even though NSLog prints msg correctly.

5
  • 2
    Are you using Automatic Reference Counting (ARC)? Commented Nov 30, 2012 at 9:11
  • Have you tried to use objectForKey instead of stringForKey? Commented Nov 30, 2012 at 9:12
  • which line does throw the exception? Commented Nov 30, 2012 at 9:13
  • 2
    instead of this tcpConnection *Obj = [tcpConnection alloc]; why did you not use the tcpConnection *Obj = [[tcpConnection alloc] init];? Commented Nov 30, 2012 at 9:15
  • Have you debugged ? in Which line it's getting exc_bad_access? Commented Nov 30, 2012 at 9:17

2 Answers 2

3

I suspect that your loadUpConnection: method isn't retaining its parameters.

It seems that there's a lot for you to learn in regards to objective-c.

I worry that this will confuse you more than help, but the msg2 variable is pointing to a static instance of the string "0001N" (because you hardcoded it at compile time the system creates a static instance to use). This is why it doesn't crash when you use msg2, but does when you use msg. msg is pointing to a dynamically allocated instance. The reference returned to you is 'autoreleased' which means it will be released at some point in the future (usually at the end of the run loop iteration). If your loadUpConnection: method doesn't retain it's parameters, then the msg string will be released before you try to use it, causing the EXC_BAD_ACCESS error. Because msg2 is a static instance, it will never be deallocated, hence it not crashing.

My only advice would be to continue learning - pick up a book, I recommend 'Programming in Objecive-C' by Stephen Kochan, or 'iPhone Programming A Big Nerd Ranch Guide' by Aaron Hillegass.

Sign up to request clarification or add additional context in comments.

4 Comments

I guess your answer is correct, but could you be more specific how to avoid this problem?
Solved it by passing msg to Obj, instead of its function. Works great now. Your answer put me on the right path, Thank YOU!
Hm. Not sure what you mean by "passing msg to Obj instead of it's function", but if it works, great! However, I still think there is a lot you still need to learn about ObjC and Cocoa/Cocoa Touch
Yes, I now that. I'm only one month in objective-c programming, but everyone has to start from zero. By passing msg to obj i mean I created property command in tcpConnection class, and after creating Obj, i have assigned Obj.command = msg. And in loadUpConnection i just used command. And it worked such way :)
1

you have to initialize you *Obj.

tcpConnection *Obj = [tcpConnection alloc] init...

1 Comment

Doing such thing does not change anything. Still craches on loadUpConnection function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.