GC-proof resource classes

cym13 via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 1 01:26:01 PDT 2015


On Monday, 31 August 2015 at 14:56:36 UTC, ponce wrote:
> 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.

By the way, why not add this to your idiom page? I think it would 
be valuable information.


More information about the Digitalmars-d mailing list