Question

When iOS memory management deallocates a given object, does the process of deallocation nil the instance variables of any properties it may have, via setter methods?

Was it helpful?

Solution

No. We can verify this experimentally with the following program:

#import <Foundation/Foundation.h>


@interface CVObject :  NSObject

@property (nonatomic) NSNumber *number;

@end


@implementation CVObject

- (void)setNumber:(NSNumber *)number
{
    NSLog(@"setting number to %@", number);
    _number = number;
}

- (void)dealloc
{
    NSLog(@"deallocating");
}

@end


void doSomething()
{
    CVObject *object = [[CVObject alloc] init];
    object.number = @3;
}


int main(int argc, const char * argv[])
{
    @autoreleasepool {
        doSomething();
    }
    return 0;
}

The output upon running it is:

setting number to 3 
deallocating

By adding another property of type CVObject* to the class, and putting a breakpoint in dealloc, we can observe the stack trace at the second object's deallocation:

* thread #1: tid = 0x2203, 0x0000000100001a77 DoesDeallocUseProperties`-[CVObject dealloc](self=0x0000000102501da0, _cmd=0x00007fff934f508b) + 23 at main.m:36, stop reason = breakpoint 1.1
    frame #0: 0x0000000100001a77 DoesDeallocUseProperties`-[CVObject dealloc](self=0x0000000102501da0, _cmd=0x00007fff934f508b) + 23 at main.m:36
    frame #1: 0x0000000100001b4a DoesDeallocUseProperties`-[CVObject .cxx_destruct](self=0x0000000100103510, _cmd=0x000f6a00000f6a00) + 90 at main.m:20
    frame #2: 0x00007fff90030fcc libobjc.A.dylib`object_cxxDestructFromClass(objc_object*, objc_class*) + 100
    frame #3: 0x00007fff9002a922 libobjc.A.dylib`objc_destructInstance + 91
    frame #4: 0x00007fff9002afa0 libobjc.A.dylib`object_dispose + 22
    frame #5: 0x0000000100001aa4 DoesDeallocUseProperties`-[CVObject dealloc](self=0x0000000100103510, _cmd=0x00007fff934f508b) + 68 at main.m:37
    frame #6: 0x0000000100001c5c DoesDeallocUseProperties`doSomething + 268 at main.m:48
    frame #7: 0x0000000100001c94 DoesDeallocUseProperties`main(argc=1, argv=0x00007fff5fbffa80) + 36 at main.m:54
    frame #8: 0x00007fff951997e1 libdyld.dylib`start + 1

It's clear from the trace no property setters or getters are being used.

Now, why might this be so? It's important to understand that properties and instance variables have no inherent connection. The names can vary independently. It is perfectly legal to have a property called name, for instance, and an instance variable _name, and implement setters and accessors for name that do not actually touch the instance variable _name. If ARC relied on properties, such a case would be disastrous.

OTHER TIPS

No it simple sends a release message to all instance variables.

This can be observed with a simple test, assuming myProp is a strong property:

-(void)dealloc
{
    NSLog(@"deallocing");
}

-(void)setMyProp:(MyClass *)prop
{
    NSLog(@"setting my prop: %@", prop);
}

You will see that the setMyProp: method is not invoked as a result of a deallocation.

You can in theory manually call setMyProp: from the overridden dealloc method but it is advised that caution be taken: Calling a method on self while in dealloc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top