Why does this call to Objective-C appear to hang?

My friend discovered some strange behavior with NSDictionary, and I'm curious why this is happening. Consider the following code:

NSDictionary *dict = [[NSDictionary alloc] init]; // Oops, we can't mutate an NSDictionary [dict setObject:[[NSNull alloc] init] forKey:@"test"]; NSLog(@"Set"); 

The code generates a compilation warning that "NSDictionary" may not respond to "setObject: forKey:". All this is good and good, and if you run it anyway, you will get this output in the console:

- [__ NSCFDictionary setObject: forKey:]: mutation method sent to an immutable object

Again, exactly what you expect. However, at the moment, the application is not crashing or terminating due to an uncaught exception. The setObject: forKey: method simply never returns, and the application seems to hang; The next NSLog never executed. If you try to step over or use a method using GDB, debugging will simply end, but without an explicit error message. The application continues to work, but the debugger does not give any idea of ​​where the execution is "stuck" in the code.

What's going on here? What does the application really do in this case, and why doesn't it crash with an NSInternalInconsistencyException or something like that?

Change For those who asked, I run Xcode 4.1 on OS X Lion (10.7.2), creating the "Apple LLVM Compiler 2.1". I use all the default settings that you get with the new Cocoa project in XCode 4. I experience the same behavior without crashing, regardless of whether I debug the program or just β€œRun” it. The transition from the Debug building to the release of the building does not matter. I can even find the .app file manually in Finder and double-click on it to execute it outside of Xcode, and it still doesn't crash.

+6
source share
1 answer

Exceptions do not cause AppKit crashes. NSApplication installs a default exception handler that catches exceptions that are not in your code. Then you just go back to runloop as usual.

Many applications exhibit this behavior. This is a common cause of inexplicable empty views / windows. If an exception occurs before the view finishes drawing, the view will be empty, but the application will not crash. Exceptions fail if you intentionally change the default exception handler for the failure.

+1
source

Source: https://habr.com/ru/post/903722/


All Articles