August 25, 2004
private import std.stream;

class foo {
public:
// Also streams etc
File bar;
this(char[] fn) {
bar = new File(fn);
}
~this() {
bar.close();
}
}

int main(char[][] args) {
// errors on runtime
foo gen = new foo("thisfile.d");
// runs
File bar;
bar = new File("thisfile.d");
return 0;
}


August 25, 2004
Joey Peters wrote:
> private import std.stream;
> 
> class foo {
> public:
> // Also streams etc
> File bar;
> this(char[] fn) {
> bar = new File(fn);
> }	
> ~this() {
> bar.close();
> }
> }
> 
> int main(char[][] args) {
> // errors on runtime
> foo gen = new foo("thisfile.d");
> // runs
> File bar;
> bar = new File("thisfile.d");
> return 0;
> }

This is a pretty well known problem with garbage collection.  When the program terminates, everything has to be destroyed, but it isn't always possible to destroy everything in the correct order, (a correct order may not even exist in the case of circular references) so destructors that access other objects inevitably run the risk of having a reference to a destroyed object.

 -- andy