GC-proof resource classes

ponce via Digitalmars-d digitalmars-d at puremagic.com
Sat Aug 29 06:58:04 PDT 2015


On Saturday, 29 August 2015 at 13:43:33 UTC, cym13 wrote:
>> But that introduce accidentally correct design when the 
>> destructor is called by GC, and avoids a leak. This is 
>> arguably worse than the initial problem.
>
> I'd like to see a concrete example of this, it seems I'm 
> missing something...
>

Example 1:

You forget to release Resource A. The GC happen to call A 
destructor that releases it. But GC destructors are not 
guaranteed to happen.
See http://dlang.org/class.html ("The garbage collector is not 
guaranteed to run the destructor for all unreferenced objects").


Example 2:

Resource A depends on Resource B. You forget to release either. 
The GC happens to call A and B destructors in the right order, by 
chance. A new D release changes this order later.







More information about the Digitalmars-d mailing list