Registered: Sep, 2005
Re: The Problem with Programming
Posted: Nov 30, 2006 8:54 AM
> Since C++ guarantees that an object's destructor will be
> called when it goes out of scope,
My technique doesn't have this requirement.
> I'm wondering what would
> be more reliable? Or are you referring specifically to
> Java or C# where you cannot rely on the garbage collector
> cleaning things up in a timely manner? I've gotten bitten
> by that in .NET at times.
This is a solution to that, but once I started using it, I've found it to be vastly superior in other ways. Mainly with separation of concerns.
> I think your method allows slightly more granular control
> but in practice I don't see how it differs substantively
> from using a locally allocated object to complete your
> task and then just letting the destructor clean up after
> What am I missing? An example would be helpful.
Java has JDBC which is an API database interaction. It's very powerful and flexible and portable to some degree but highly procedural in design (IMO).
I created a think abstraction around this for my work. Basically, I have a manager class that takes a query object and a handler, it executes the query and passes each row back to the handler. The manager guarantees that the connection is cleaned up (or not, it's transparent to the 'client'). This happens even if the handler or query objects are never released. It happens as soon as the handler returns after the last row, blows up, or signals that it wants no more rows. Obviously, if the handler never returns, I'm stuck but that's not a problem I've seen yet and is a problem regardless of this design.
It's perfectly acceptable for the client to hold onto queries for the life of the application (rows and handlers too but there is little point). It actually encouraged because the queries can be compiled on the database and rerun with different parameters, and connections refreshed, statements recompiled as needed and is transparent to the client class.
With RAII, you essentially have a requirement that the object be released. I don't know if or how you document this behavior in APIs, but it's an unnatural side-effect, if you ask me.