Reference counting for resource management

Bartosz Milewski bartosz-nospam at relisoft.com
Mon Nov 30 12:34:04 PST 2009


Denis Koroskin Wrote:

> I think RefCounted should be flexible enough to re-usable for other data  
> structures, too (e.g. so that it could be part of Phobos).
> 

I agree. However this code goes into core, so it can't use Phobos.

> By the way, your code has a bug:
> 
> Tid tid; // or release() and exisiting Tid
> Tid tid2 = tid; // segfault since you don't check if _cnt is null in  
> postblit

I realize that, but I'm not sure if this would be a library bug or a user bug. I wish one could just prevent such usage, e.g., by nulling the default constructor:

struct Tid {
    this() = null;
    ...
}

This is a more general problem of the absence of user-defined default constructors for structs. This makes perfect sense for PODS, but not for structs that have a destructor (and postblit). Of course, if the default constructor is blocked, you wouldn't be able to have arrays of such structs, which, as a side effect, would eliminate the problem of exceptions thrown from default constructors and array unwinding. It's a bit of a minefield there.




More information about the Digitalmars-d mailing list