2

I have some code that is supposed to return an NSString. Instead it is sometimes returning an NSArrayReverseEnumerator. This causes an unrecognized selector sent to instance exception.

NSString *tFirstName = nil;
NSString *tUsername = ((NSString*)[self.userObject objectForKey:USER_NAME]).length > 0 ? [self.userObject objectForKey:USER_NAME] : [self.userObject objectForKey:USER_USERNAME];
NSRange spaceRange = [tUsername rangeOfString:@" "];
if (spaceRange.location == NSNotFound) {
    tFirstName = tUsername;
} else {
    tFirstName = [tUsername substringToIndex:spaceRange.location];
}
return tFirstName;

I'm then setting a UITextField text property to what is returned which causing an exception.

The exception is:

-[__NSArrayReverseEnumerator isEqualToString:]: unrecognized selector sent to instance 0xf1b8190

Here is the last few lines of the stack trace:

 CoreFoundation 0x381208bf __exceptionPreprocess + 163
1 libobjc.A.dylib 0x31c061e5 objc_exception_throw + 33
2 CoreFoundation 0x38123acb -[NSObject doesNotRecognizeSelector:] + 175
3 CoreFoundation 0x38122945 ___forwarding___ + 301
4 CoreFoundation 0x3807d680 _CF_forwarding_prep_0 + 48
5 UIKit 0x354dc20d -[UILabel setText:] + 45
6 Trivial 0x000873c9 -[vcGameOver viewDidLoad] (vcGameOver.m:45)
7 UIKit 0x355117ff -[UIViewController view] + 167

userObject looks like this normally, haven't been able to reproduce in the debugger yet to see what it looks like at the time of exception:

userObject looks like this:

(PFUser *) $1 = 0x0be86fb0 <PFUser:MTsJR1DKuS> {
    answeredCorrect = "<PFRelation: 0xa847f30>";
    centsEarned = "-2353";
    centsPaidFor = 5000;
    centsUsed = 4193;
    correctAnswers = 1454;
    earnedCoinDate = "2012-06-12 19:42:44 +0000";
    eloRating = 1262;
    iq = 119;
    lastPlayedDate = "2012-06-12 19:15:46 +0000";
    locale = en;
    losses = 10;
    name = "Matt Hudson";
    status = 0;
    username = Inturbidus;
    wins = 8;
    wrongAnswers = 1157;
} 

My question is, which line in my code above could possible return an NSArrayReverseEnumerator?

Matt Hudson
  • 7,329
  • 5
  • 49
  • 66

1 Answers1

0

You may have a dangling pointer, with some other object (in this case, the presumably private-to-Apple __NSArrayReverseEnumerator) squatting in the location in memory where your prematurely released NSString instance used to be.

I would suggest turning on zombies (NSZombieEnabled) and seeing if you get a "message sent to deallocated instance" error message instead of the one you got.

inwit
  • 935
  • 6
  • 11
  • I have Zombies enabled but I don't get the error in the simulator. I guess I need more testing. Can you point me to a web site that talks about dangling pointers? – Matt Hudson Jun 13 '12 at 15:23
  • @inturbidus If you don't get the "`message sent to deallocated instance`" error with zombies enabled but continue to get your original "`unrecognized selector sent to ...`" error, it may be something else like a buffer overrun. For example, writing outside the bounds of a memory buffer you have malloc'ed or indexing a C array beyond its declared size. – inwit Jun 13 '12 at 18:06
  • @inturbius Take a look at this other stackoverflow thread which also features `__NSArrayReverseEnumerator` (http://stackoverflow.com/questions/4848137/debugging-over-released-objects-problem-with-nszombie). – inwit Jun 13 '12 at 18:25
  • This hint may have helped me out just enough to see that I had an assign where it should've been a strong causing it to be a dangling pointer. – Matt Hudson Jun 14 '12 at 03:35