www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - If I'm not being retarded than I might have found a bug or something

reply Joey Peters <Joey_member pathlink.com> writes:
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;
}
Aug 24 2004
parent Andy Friesen <andy ikagames.com> writes:
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
Aug 25 2004