GC-proof resource classes

skoppe via Digitalmars-d digitalmars-d at puremagic.com
Sat Aug 29 09:12:45 PDT 2015


On Saturday, 29 August 2015 at 13:14:26 UTC, ponce wrote:
> class MyResource
> {
>     void* handle;
>
>     this()
>     {
>         handle = create_handle();
>     }
>
>     ~this()
>     {
>         if (handle != null) // must support repeated calls for 
> the case (called by .destroy + called by GC later)
>         {
>             ensureNotInGC("MyResource");
>             free_handle(handle);
>         }
>     }
> }

I don't think it is a good idea to call create_handle() in the 
constructor. Why not just pass a handle into the Resource?


More information about the Digitalmars-d mailing list