0

I'm making this function that takes a string input, converts it to an int, and finds the binary equivalent. It was working an hour ago, but after i tried tinkering with a different keypad, and then going back, it no longer works:

 - (void)convertToBinary:(NSString *)tf
    {
      NSString *result = @"";
      NSLog(@"-- %@", tf); //successfully prints
      [tf retain];
      if ([tf isKindOfClass:[NSString class]]){
        NSLog(@"tf is NSString"); //THIS PRINTS SO TF IS CONFIRMED TO BE NSSTRING
       }
      int dec = [tf intValue]; //BREAKS HERE...!

      if (!dec){
        [binaryOutput setText:@"Sorry, invalid input!"];
        } else {
              ...
        }

}

I know that to convert an NSString to an int, i just need to do int dec = [someString intValue] yet that isn't working. The error i'm getting is EXEC_BAD_ACCESS

Any help please?

EDIT

I just made a small command-line project:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...
    NSString *t = @"123";
    int d = [t intValue];
    NSLog(@"Hello, %@", d);
    [pool drain];
    return 0;
}

and the EXEC_BAD_ACCESS still persists for such a simple program! What's the problem here?

Matthew
  • 2,035
  • 4
  • 25
  • 48
  • The reason why it crashes in your command-line project is, that you use the wrong format string. use @"Hello, %d" instead of @"Hello, %@" – Thomas Zoechling May 12 '11 at 11:52

4 Answers4

1

EXEC_BAD_ACCESS usually fires when there is a memory leak, check that tf is not null or released. You can write NSLog(@"%@", tf); at the beginning of the method to check that.

albianto
  • 4,092
  • 2
  • 36
  • 54
  • Just check tf with NSLog, and it prints out fine. My method is very simple and doesn't use any alloc. Would i need to retain or copy? – Matthew May 12 '11 at 11:29
  • no luck. I made a simple helloWorld where i convert a string to an int and the problem persists. Your two cents? – Matthew May 12 '11 at 11:46
  • 1
    EXEC_BAD_ACCESS is not the result of a leak, but most likely caused by sending a message to an already released object. A leak happens if you have lost reference to an object. – Thomas Zoechling May 12 '11 at 11:48
1

As edo42 it's probably related to a null/released string. See "EXC_BAD_ACCESS signal received" for lots of information about that error message, and how to debug using NSZombieEnabled.

Community
  • 1
  • 1
Alex Ghiculescu
  • 7,522
  • 3
  • 25
  • 41
1

Instruments.app with the Zombies instrument might help to uncover the crash. Also try a "Build and Analyze" run.

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
1

You should be using "%d" when printing integers. Change the line to:

NSLog(@"Hello, %d", d); 
quaertym
  • 3,917
  • 2
  • 29
  • 41