Design patterns for resource management



I'm more of a Java person than a C++ guy, but I'm becoming increasinly aware
of the contortions Java is forcing me into because of a lack of destructor
(partly because I'm having to defend them to C++-centric coworkers). I
understand the performance reasons for using the purer GC model, but
I'd give them up in a lot of cases for easier-to-manage scoping of resource
use.

For Connection, Socket, and other heavy resource-consuming objects, the
standard Java pattern is try/finally - acquire the resource in a try and
release it in a finally. The problem is that this is NOT required for most
objects, and it leaks implementation details up a level: now a user of an API
needs to treat an object very differently based on what resources it uses.

In a lot of cases, I'd much prefer C++ style scoping, where an object can be
created, used, and it will automatically be destructed when the scope leaves.
The user of the object doesn't need to know that cleanup is needed, and
therefore an explicit try/finally.

Every time I've gone down the road of making an API more encapsulated WRT
whether or not it holds heavyweight resources, I end up causing more bugs than
I prevent, and go back to the simple methodology of requiring my caller to
know that close() is required.

Anyone have any suggestions for better patterns, or should I just get over
this and learn to love try/finally and explicit close() methods?
--
Mark Rafn dagon@xxxxxxxxx <http://www.dagon.net/>
.



Relevant Pages

  • Re: MFC future?
    ... all ILs assume the programmer wants less control ... ... when people look at IL-based systems today they tend to be thinking of Java ... In Java there can be serious resource management problems because the ...
    (microsoft.public.vc.mfc)
  • Re: Newbie question - Disadvantages of java
    ... Smalltalk to Java and encountered the following problems immediately ... Collection handling a little awkward requiring ... GUI builders easy to use but editing GUI for clients ... standard so only useful for short lists unless adding JScrollPanes. ...
    (comp.lang.java.programmer)
  • Re: Lock-free reference counting
    ... Memory is not the only resource that can be reserved and freed. ... Some languages offer tools to help these cases, ... In Java I think the ... last reference to it dies" effect using those commands if the object is ...
    (comp.programming)
  • Re: Copying resources
    ... Therefore you are trying to use a "very" generic placeholder that can then ... combination of resources for the Java Team, you may use Java Developer 10, ... Putting a place holder resource name in ... >> If I understand Terry's issue correctly, she's trying to use a place holder ...
    (microsoft.public.project)
  • Re: This says it all
    ... >> resources in the constructor and releasing them in the destructor? ... it would be simpler to just get the resource I ... Java purports to be an object-oriented language. ...
    (comp.lang.cpp)