Notes on Defective C++

Jim Hewes jimhewes at
Mon Dec 8 17:12:51 PST 2008

"Christopher Wright" <dhasenan at> wrote in message 
news:ghkdnd$e95$1 at
> And what are you going to throw an exception from, besides a function? I 
> think you are talking about situations like this:
> class A
> {
>    private File file;
>    this () { file = new File ("somePath"); }
>    // some operations with side effects that maybe close the file
> }
> void foo ()
> {
>    auto a = new A;
>    // I want to make sure A's file is cleaned
> }

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?

Could a garbage-collected language ever figure out how to handle this? When 
class A goes out of scope, how would it know that the file object is not 
being referenced elsewhere? I wonder if there's a way that reference 
counting could be used in these cases.


More information about the Digitalmars-d mailing list