objective c - Calling a method on self while in dealloc -
i have dictionary of objects need cleaned before released. have method entire dictionary. before release dictionary in -dealloc
method, want same thing. however, not sure of state of object during deallocation. in c# or java, instance, never call method on object being finalized, not sure applies objective c , deallocation. acceptable call clean method on self
during deallocation, or should duplicate functionality in -dealloc
?
yes, may invoke methods inside dealloc
method, though you're wise cautious. pretty only methods should invoke should "tear down" methods, or methods in cleaning object before resources reclaimed. of these cleanup methods include:
- unregistering notifications via notification center
- removing key-value observer
- other general cleanup methods
note, however, in each of these methods, object in inconsistent state. may partially deallocated (some ivars may/will invalid), , should never rely on specific object state. these methods should only used continue deconstructing object state.
this fundamental reason why we're discouraged using property setters (setfoo:
methods) in dealloc
: object may registered observer, , using property trigger kvo notification, , if observer expect object have valid state, out of luck , things can blow quickly.
tl;dr:
yes, it's safe, long you're smart it.
Comments
Post a Comment