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.

Reply via email to