Will An Associated Object Be Released Automatically?

前端 未结 4 1275
南笙
南笙 2020-12-12 17:40

Note: This other question seems relevant but it\'s not: When does an associated object get released?

I\'m adding a second description to a UIView instan

相关标签:
4条回答
  • 2020-12-12 18:09

    Section 4 in Jody Hagins' answer says "Erase associated references", which doesn't explicitly imply that the references are released. So I used the following piece of code (note WITHOUT ARC) to test this.

    @interface AssociatedObjectHelper : NSObject
    @end
    
    @implementation AssociatedObjectHelper
    - (void) dealloc
    {
        NSLog(@"In %s", __FUNCTION__);
        [super dealloc];
    }
    @end
    
    @implementation AppDelegate
    ...
    - (void) testReleaseAssociatedObject
    {
        static const NSString *key = @"testKey123";
        NSObject *ob = [NSObject new];
        AssociatedObjectHelper *assocOb = [AssociatedObjectHelper new];
        objc_setAssociatedObject(ob, key, assocOb, OBJC_ASSOCIATION_RETAIN);
        [assocOb release];
        [ob release];
    }
    

    Invoking above code does indeed end up calling -[AssociatedObjectHelper dealloc], with the following stack-trace:

    #0  0x000000010000528f in -[AssociatedObjectHelper dealloc]
    #1  0x00007fff8a0bb89c in objc_object::sidetable_release(bool) ()
    #2  0x00007fff8a0a537f in _object_remove_assocations ()
    #3  0x00007fff8a0a1644 in objc_destructInstance ()
    #4  0x00007fff8a0a1595 in object_dispose ()
    #5  0x00007fff8a0bb89c in objc_object::sidetable_release(bool) ()
    #6  0x000000010000e9b6 in -[AppDelegate testReleaseAssociatedObject]
    

    Tested on Xcode 7.0.1

    0 讨论(0)
  • 2020-12-12 18:12

    Yes. When an object is dealloc'd, any associated objects (that use the RETAIN or COPY association types) are automatically released.

    0 讨论(0)
  • 2020-12-12 18:22

    If you want to actually see the description of the entire dealloc timeline, look at WWDC 2011, Session 322, 36:22. However, here's the basic rundown (I wanted to remember it, so this is an actual comment in a piece of my code).

    Note, that the associated objects are released at the end of the life cycle.

    // General Information
    // We take advantage of the documented Deallocation Timeline (WWDC 2011, Session 322, 36:22).
    // 1. -release to zero
    //     * Object is now deallocating and will die.
    //     * New __weak references are not allowed, and will get nil.
    //     * [self dealloc] is called
    // 2. Subclass -dealloc
    //     * bottom-most subclass -dealloc is called
    //     * Non-ARC code manually releases iVars
    //     * Walk the super-class chain calling -dealloc
    // 3. NSObject -dealloc
    //     * Simply calls the ObjC runtime object_dispose()
    // 4. object_dispose()
    //     * Call destructors for C++ iVars
    //     * Call -release for ARC iVars
    //     * Erase associated references
    //     * Erase __weak references
    //     * Call free()
    
    0 讨论(0)
  • 2020-12-12 18:27

    In short, yes - when the owning object is released then retained associated objects are released. See the first section of Apple's documentation

    0 讨论(0)
提交回复
热议问题