Usually for objects that have some state related to a resource without memory, to provide a method for explicitly "terminating" this resource. Is there a preferred common practice to consider when an attempt to free an object is made while maintaining the “use my resource” state? I have seen several different approaches:
- log that the programmer made a mistake and told them how to debug it (
NSLock does this) - let the caller choose whether the resource is being managed by the caller or refuses to free (for example,
NSFileHandle ) - throws an exception if the object is not in the expected final state, i.e. claims that the programmer will not use my object in this way
- always get out.
GC docs confirm that managing other resources along with memory management is a “Bad Idea” and (for now) - Example 4, albeit with reservations. So did someone choose the approach that always follows?
user23743
source share