It's usual for objects which have some state related to a non-memory resource to provide a method for explicitly 'finishing' with that resource. Is there a preferred common practice for dealing with the case where an object deallocation is attempted while still in the "using my resource" state? I have seen a couple of different approaches:
- log that the programmer made a mistake and tell them how to debug it (
NSLock
does that) - let the caller choose whether the resource is managed by the caller or relinquished on deallocation (e.g.
NSFileHandle
) - raise an exception if the object is not in an expected final state, i.e. assert that the programmer isn't going to use my object in that way
- always clean up on the way out.
The GC docs confirm that managing other resources alongside memory management is A Bad Idea and (yet) provide an example of doing 4, albeit with caveats. So has anyone "chosen" an approach to always follow?