August 31, 2015
On Monday, 31 August 2015 at 12:54:05 UTC, Sebastiaan Koppe wrote:
> What about:
>
> ```
> class MyResource
> {
>     void* handle;
>     this()
>     {
>         handle = create_handle();
>     }
>     close()
>     {
>         if (handle)
>             free_handle(handle)
>         handle = null;
>     }
>     ~this()
>     {
>         enforce(!handle,"Resource leak");
>     }
> }
> ```



Unique!T destructor calls delete which calls ~this() not close()
RefCounted!T and scoped!T call .destroy which calls ~this() not close()
We really want one thing there.
August 31, 2015
On Monday, 31 August 2015 at 13:35:54 UTC, ponce wrote:
> On Monday, 31 August 2015 at 12:54:05 UTC, Sebastiaan Koppe wrote:
>> What about:
>>
>> ```
>> class MyResource
>> {
>>     void* handle;
>>     this()
>>     {
>>         handle = create_handle();
>>     }
>>     close()
>>     {
>>         if (handle)
>>             free_handle(handle)
>>         handle = null;
>>     }
>>     ~this()
>>     {
>>         enforce(!handle,"Resource leak");
>>     }
>> }
>> ```
>
>
>
> Unique!T destructor calls delete which calls ~this() not close()
> RefCounted!T and scoped!T call .destroy which calls ~this() not close()
> We really want one thing there.

I normally stick with this pattern when dealing with resource, though I would only uses a class if I needed an interface or inheritance..


```
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();
    }
}
```


August 31, 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.
September 01, 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.
September 01, 2015
On Tuesday, 1 September 2015 at 08:26:02 UTC, cym13 wrote:
>
> By the way, why not add this to your idiom page? I think it would be valuable information.

Already there! http://p0nce.github.io/d-idioms/#GC-proof-resource-class
1 2 3 4
Next ›   Last »