I am writing an ARC-enabled framework that creates a hierarchy of objects, as opposed to a Cocoa view hierarchy. Each controller object can have several subcontrollers. Controllers may have links to each other, which creates a potential risk of creating a retention cycle.
I know how to avoid save cycles. I want to know if I have a way to programmatically determine that there is a save cycle and is preventing an object from being freed?
At some point, the existing root controller will be replaced by the new root controller. Since I use ARC, I cannot use keepCount to verify that the current control counter is holding the count. Which should not be trusted in any way from what I read.
I have a test setup in which the root controller has two controllers under control, and each of them has a strong link to the other. In this case, the root controller does not start dealloc, and the other two controllers when the root controller is replaced with a new controller. As expected. I thought that given this scenario, there should be some way to determine if this root controller really frees up or not.
Possible solution: I assigned the root controller, which will be replaced, to the "zero" property on the global object shortly before replacing the controller. Then I set the timer so that after a split second I would check if the property is zero or not. If it is zero, the controller will shut down. If it's not zero, it probably indicates a memory leak, which was probably caused by a save loop somewhere in the hierarchy. In this case, I print the log statement until the replaced controller is zero to get the attention of the developer.
This works, but are there any alternative (better) solutions? Or possible reservations with this decision?
In particular, how long can it take before an object is canceled - is it guaranteed instantly or can release be canceled, and if so, for how long?