Since the variable b completely internally class A and not declared as @public (the default is @protected ), the best way to find out if b is freed is to free it and set the pointer to nil . That way you can just check if (b == nil) and create a new instance if necessary.
A more general question is what is the true nature and behavior of memory b . You state that “Object B can be freed at low levels of memory,” but do not explain why. If you use standard idioms to save-release in Objective-C, I would think that A will determine if B should be freed as it creates a new instance. If you do not intend for A to make such decisions, allowing him to allocate a new B, this will lead to ownership confusion and memory errors in the future.
If A is not responsible for B, but if you are on Leopard (or beyond) and turn on garbage collection, then you may need to reset the weak links . This is declared using __weak before declaring the instance variable and does not prevent the garbage collector from collecting the object it points to. (A strong link is the default, and the garbage collector does not free an object that it can only trace from the root through strong links.) In addition, GC automatically sets weak links to 0 for you if / when the object is redistributed.
Returning to the “bigger question”, if b already a weak benchmark and GC is turned on (and a few more conditions), the system will not automatically release you from B. If the reason for freeing b is because instances of B grow over time (for example, cache of dispensable elements), it would be much better to have a way to empty B. For example, mutable collections in Foundation have -removeAllObjects . Such an approach would have avoided more confusion as to whether b still exists and is much more efficient than repeatedly (de) selecting objects.
Quinn taylor
source share