Garbage collector and real resources

AlexBurton alexibu.spampreventingextrabit at mac.com
Wed Jan 24 15:55:03 PST 2007


Hi,

First I must confess that I come from C++. I am hoping to move most of my code in the future to D, and am finding it a great language.

One problem which I hope someone can help me with is where classes reference real resources.

Here is some example code which I think illustrates my problem

import std.stream;

class FileWriter
{
	std.stream.File f;
	this()
	{
		 f = new std.stream.File("data.txt",FileMode.Out);
		 f.write(23);
	}

};

int main()
{
	{
		scope auto f = new FileWriter();
	}

	{
		scope auto f = new FileWriter();
	}

	return 0;
}

In this case the FileWriter class uses a real and finite resource (unlike memory which a GC can handle quite well)

The second instance of FileWriter fails because the first is still around even though the scope keyword is used.

A solution is to write a destructor for FileWriter that uses the delete keyword (and still use scope).
Or to leak the close method of the FileStream - and write a try catch block for each level of function call that can exist higher in the stack.

But what happens if the FileWriter is an aggregate part of another larger class, the explicit destructors and scope decrlarations are leaking the implementation detail that this class has a real resource.

Also I note that the scope keyword can't be put in class scope ...

This is a general problem that ties in conceptual constness - it would be nice if there were a way to express that an object is an aggregate part of another.

Please tell me if I am missing something (including possibly some fundamental GC philosophy)

Alex



More information about the Digitalmars-d mailing list