RAII support

Tristam MacDonald swiftcoder at gmail.com
Wed Jul 11 07:57:19 PDT 2007


Now that the whole const issue seems to have died down, I wanted to get a few thoughts on what people are using to work arround the missing RAII support. So an overview of what I *think* is the current situation, please correct me if I am wrong:

Basically, the only tool for RAII is 'scope' reference/classes. They are very handy for locks (mutexs, etc.) but they are not so useful for general resource management, since they cannot be returned from functions, or stored somewhere.

The *only* use of destructors on normal (i.e. non-scope) is to free malloced memory - While the program is running. Non-malloced memory is GC'd anyway, and you can't use the destructor to free other resources, because it is not guarenteed to ever be called (and often isn't called when the program exits).

So the only method for managing shared memory, file handles, etc. is to perform explicit reference counting (i.e. AddRef()/DeRef()), just like in plain old C. If this were C++, we could wrap the AddRef()/DeRef() calls in a handle struct, which would automate the reference counting, but unfortunately, D still doesn't have constructors/destructors/assignment for structs (i.e. value types), so we are stuck with C-style manual ref counting.

And that about covers it... Is anything planned (in the *near* future) to fix this? And does anyone have hacks/gimmicks for working around this (other than not having shared resources)?



More information about the Digitalmars-d mailing list