Reputation: 2202
I have a very weird issue going on. I made a mistake while defining an NSString object. It is a clear bug. But the weird thing is that it works fine on some iOS devices, while it causes crashes on others.
I have fixed the bug, but I am still in the dark as to why it would have ever worked.
If anyone can help me understand this it would be very appreciated.
Thanks!
Bad Code:
NSString *language2 = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *language = [language substringToIndex:2];
NSLog(@"language = %@", language);
As you can see, "NSString *language = [language substringToIndex:2];" is defined as a substring of itself.
What's crazy is that this works on some devices. I'm guessing it is a 64/32 bit thing that I just don't understand.
It crashes on an older iPad. While on a newer iPhone6, it works and the NSLog indicates "language = en".
Any ideas?
Upvotes: 2
Views: 203
Reputation: 162712
If you have ARC enabled, then all local variable will be initialized to NULL prior to use. So, do you have ARC enabled? Also, there is some pretty radically different codegen between a 32 bit and 64 bit target, including how the stack grows. It may be that you've "gotten lucky" and the object reference fell into the right spot on the stack due to a spill out of registers on a call.
If you want to explore more, try using NSLog() to print the address of the object (@"%p"
). Though, more likely than not, you'll cause different behavior.
All in all, you are playing with undefined behavior and it is behaving in an undefined fashion correctly. :)
Upvotes: 1
Reputation: 147
I do not think that this is the think that causes confusion since I tried EXACT same code on different devices, real devices and emulator devices, and different iOS versions. Only thing that I got StackOverExpObjectiveC[] (null)
which is actually expected. If you could share more of your project maybe found the base of weird stuff...
Upvotes: 0