Notes on Defective C++

Christopher Wright dhasenan at gmail.com
Tue Dec 9 04:22:04 PST 2008


Jim Hewes wrote:
> Yes. Thanks for the example. I do that sort of thing a lot, and it 
> applies to anything with a handle such as mutexes, files, etc.  In 
> garbage-collected languages, what am I supposed to do there? It would 
> seem that garbage collection and exceptions don't play nice together. Or 
> am I missing something simple?

In this case, File should close itself in its destructor. Class A can't 
close its file in its destructor because the file might have already 
been collected and finalized. And you can't use a scope guard in this 
case because A doesn't let you just close the file.

I'd say that, if File doesn't close itself in its destructor, that's an 
error. If File has this error, A should offer a way to close the file 
without side effects.



More information about the Digitalmars-d mailing list