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