GC-proof resource classes

ZombineDev via Digitalmars-d digitalmars-d at puremagic.com
Sun Aug 30 14:52:35 PDT 2015


On Sunday, 30 August 2015 at 20:44:17 UTC, ponce wrote:
> In the case the destructor isn't called by the GC, the call 
> must succeed.
> GC.malloc(1) fits the bill but it's a waste of time and memory 
> indeed. GC.free(<invalid-adress>) would fail in both cases if I 
> understand correctly.

As you can see from the output 
(http://dpaste.dzfl.pl/aa004554034a), when GC.free(cast(void*)1) 
is called in main() it doesn't throw. It only throws in the 
destructor of A, because a GC collection is taking place.

If you look at the implementation, it is more or less guaranteed 
that:
a) GC.free() during collection -> InvalidMemoryOperationError [1]
b) GC.free() with invalid pointer -> no-op [2]

> Perhaps as a getter like GC.isRunning()?

Yeah, that's what I had in mind. But maybe it also makes sense to 
provide a way to take the GC lock? Of course, such method should 
definitely be marked as @system.

[1]: 
https://github.com/D-Programming-Language/druntime/blob/master/src/gc/gc.d#L879
[2]: 
https://github.com/D-Programming-Language/druntime/blob/master/src/gc/gc.d#L888


More information about the Digitalmars-d mailing list