Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c example code that intentionally messages a deallocated object

I'm new to objective-c and xcode and an app I'm currently writing is receiving the infamous EXC_BAD_ACCESS error.

Almost everybody recommends starting to solve the problem using NSZombies. I think I have NSZombies working but xcode isn't giving me an alert about a zombie being messaged when my app crashes.

Before I move on with my debugging, I'd like to run some code that should for sure result in a message being sent to a zombie (de-allocated object).

What is a simple code snippet where a message is sent to a deallocated object, causing a scenario where NSZombies should alert me?

like image 996
Alex Crist Avatar asked Aug 14 '15 17:08

Alex Crist


People also ask

What language is Objective C used for?

Objective-C Tutorial. Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. This is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch.

What is Objective-C programming language for iOS?

Objective-C is the primary coding language in iOS development. For every iOS coding beginner, it is important to learn the hello world example first. This article will introduce Objective-C’s basic programming grammar, code structure and commonly used classes with examples. 1. Objective-C Hello World.

What is the difference between alloc and init method in Java?

5.3 Use alloc and init Method Manually. alloc is a class method and init is a object method. alloc method just return an object of the class, init method will initialize all the object properties value to 0 by default. Please Note : After the default initialization is complete, all member variables value will be initialized to 0.

How to access the static properties in Objective-C?

The static properties in objective-c are private global variables. It can not be accessed directly by the class name. It only works in the .m file where it is declared. Although the static property is private, but they can be accessed externally by static methods.


2 Answers

For non-ARC code:

- (IBAction) messageZombie:(id)sender {
    id a = [[NSObject alloc]init];
    [a release];
    NSLog(@"%@", [a description]);
}

This will give you EXC_BAD_ACCESS with Zombies off, and a "message sent to deallocated instance" message, with Zombies enabled.

If your project is using ARC, then it's a bit harder to reliably-cause messages to de-allocated objects (that is the point of ARC, after all).

This works:

- (IBAction) messageZombie:(id)sender {    
    id a = [[NSObject alloc]init];
    id __unsafe_unretained b =a;
    a=nil;
    NSLog(@"%@", [b description]);
}

It's probably not very similar to what your actual code is doing, because who the heck uses __unsafe_unretained, anyway? But if you just want to make sure that you've got NSZombies turned on properly, this should be a reasonable test case.

If you're looking for suspicious places in your code, then for sure look for __unsafe_unretained pointers, though you won't find any*, and double-check that the right casts are used for CoreFoundation objects that are casted to Cocoa objects.

* If your project needs to support OS X versions before 10.7, or iOS versions earlier than 5.0, then you can't use __weak pointers, so in that sort of project, you'd expect to find __unsafe_unretained used more often.

like image 192
Mark Bessey Avatar answered Nov 25 '22 11:11

Mark Bessey


You could create a CF object, bridge it to an Objective-C object, then release it and try to use the bridged object. I think you have to use __bridge to get this to behave the way you want.

like image 45
Tim Johnsen Avatar answered Nov 25 '22 10:11

Tim Johnsen