GC-proof resource classes

ponce via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 31 07:56:34 PDT 2015


On Monday, 31 August 2015 at 14:18:43 UTC, byron wrote:
>
> ```
> class MyResource
> {
>     void* handle;
>     this()
>     {
>         handle = create_handle();
>     }
>
>     close()
>     {
>         if (handle !is null)
>         {
>             synchronized {
>                 if (handle !is null) {
>                     free_handle(handle);
>                 }
>             }
>             handle = null;
>         }
>     }
>
>     ~this()
>     {
>         close();
>     }
> }
> ```

Used to do like that modulo the synchronized (which makes sense 
considering destructors are run after all threads are unpaused). 
The problem is that relying on GC destructors tends to come bite 
later imho.


More information about the Digitalmars-d mailing list