Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this crash?

Tags:

People also ask

Why is Google Chrome hanging?

Your computer may have run out of memory, and can't load the site while also running your apps, extensions, and programs. To free up memory: Close every tab except for the one that's showing the error message. Quit other apps or programs that are running.

How do I stop Google Chrome from crashing?

Google Chrome Keeps Crashing: Basic Troubleshooting When Chrome starts crashing or freezing, you should first try completely restarting it. To do so, click the three-dot Menu button at the top-right of Chrome and choose Exit. Then reopen Chrome after a moment and see if the issue improves.

What causes Google Chrome not responding?

It's always possible something was corrupted, or the combination of settings caused a problem. The only way to know for sure is to reset everything to the way it was when you installed Chrome the first time. Reinstall Chrome. If it seems like nothing works, reset Chrome to default, uninstall it, and install it again.

Why some websites are not opening in Chrome?

The reason why Chrome is not loading pages may be down to something as simple as unstable or lost internet connection. Ensure that you have an active data plan, and restart your internet connection. Also, try loading other browsers and apps such as Firefox and WhatsApp.


I'm attempting to narrow down a bug to a minimum reproducible case and found something odd.

Consider this code:

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

    if (staticString == nil) {
        staticString = [[NSArray arrayWithObjects:@"1", @"2", @"3", nil] componentsJoinedByString:@","];
    }   

    [pool drain];

    NSLog(@"static: %@", staticString);
    return 0;
}

I'm expecting this code to crash. Instead, it logs:

2011-01-18 14:41:06.311 EmptyFoundation[61419:a0f] static: static: 

However, if I change the NSLog() to:

NSLog(@"static: %s", [staticString UTF8String]);

Then it does crash.

edit a bit more info:

After draining the pool:

NSLog(@"static: %@", staticString);  //this logs "static: static: "
NSLog(@"static: %@", [staticString description]); //this crashes

So apparently invoking a method on the string is good enough to get it to crash. In that case, why doesn't logging the string directly cause it to crash? Shouldn't NSLog() be invoking the -description method?

Where is the second "static: " coming from? Why isn't this crashing?


Results:

Both Kevin Ballard and Graham Lee are correct. Graham's correct in realizing that NSLog() is not invoking -description (as I was erroneously assuming), and Kevin is almost definitely correct that this is a weird stack-related issue with copying a format string and a va_list around.

  1. NSLogging and NSString does not invoke -description. Graham elegantly showed this, and if you trace through the Core Foundation sources that do the logging, you'll see that this is the case. Any backtrace originating inside NSLog shows that it invokes NSLogv => _CFLogvEx => _CFStringCreateWithFormatAndArgumentsAux => _CFStringAppendFormatAndArgumentsAux. _CFStringAppendFormatAndArgumentsAux() (line 5365) is where all of the magic is going on. You can see that it's manually going through to find all the % substitutions. It only ends up invoking the description copy function if the type of the substitution is a CFFormatObjectType, the description function is non-nil, and the substitution hasn't already been handled by another type. Since we've shown that the description is not getting copied, it's reasonable to assume that an NSString gets handled earlier (in which case it's probably going to be doing a raw byte copy), which leads us to believe...
  2. There's a stack error going on here, as Kevin surmises. Somehow the pointer that was pointing to the autoreleased string is getting substituted to a different object, which happens to be an NSString. So, it doesn't crash. Weird. However, if we change the type of the static variable to something else, like an NSArray, then the -description method does get called, and the program does crash as expected.

How truly and utterly strange. Points go to Kevin for being the most correct about the root cause of the behavior, and kudos to Graham for correcting my fallacious thinking. I wish I could accept two answers...