Asynchronous methods, called inside `-dealloc`, can generate unwanted zombie objects

Since I was going through some lines of code, I came across this problem a couple of days ago,

- (void)dealloc {
    ...
    [self.postOfficeService deregister:self];
    ...
}

If de-registering from the mail branch service is an asynchronous operation, even if it is not obvious from the interface, since there is no block or function passed to postOfficeService.

The internal implementation of the postOfficeService method -deregisteris something similar

// -deregister:(id)formerSubscriber implementation
//some trivial checks here
// deregister former subscriber
dispatch_asynch(_serialQueue, ^{
    [self.subcribers removeObject:formerSubscriber];
});
...

The container self.subscribersdoes its job perfectly and contains only weak links. That is it NSHashTable.

, dealloc, , postOfficeService , ., [self.subscribers removeObject:formerSubscriber], , formerSubscriber NSZombieObject. .

, deregister - , dispatch_synch dispatch_asynch , , dealloc.

, NSZombie, ARC, - - NSHashTable ( , )?

+4
4

: dealloc, , dealloc ( release, 0), .

ARC dealloc - ; dealloc , ( dealloc , / dealloc). ARC , .

+2

ARC , .

,

  • [obj release], ARC

    • [obj dealloc]

      • [obj.postOfficeService deregister: obj]
        • [obj ] - , .
        • dispatch_async
    • free (obj) - obj -

  • GCD

  • dispatch_async
    • obj - crash

dispatch_sync, , . ( )

0
  • dealloc. . -deregister .
  • NSHashTable - __unsafe_unretained assign - UNLESS, +weakObjectsHashTable (NSHashTableZeroingWeakMemory NSPointerFunctionsObjectPersonality). , , , .

, " ", , Zombies ​​ .

0

, , , -dealloc. , -deregister: __unsafe_unretained. . . , NSHashTable . NSHashTable , -removeObject: , ARC , .

API- C - (, NSHashRemove()), NSHashTable.

0

All Articles