www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - reference counting resources but NOT memory

reply Tove <tove fransson.se> writes:
Is there any elegant/smart solution to reference counting 
resources without ever freeing any objects?

When the ref hits 0 it should free some other resource that isn't 
memory...

Resource[10] resources;

resources[3].inc; // 1 ref - 0->1 transition enable  some feature
resources[3].dec; // 0 ref - 1->0 transition disable some feature
resources[3].inc; // 1 ref - instantly ready to be used again 
without any alloc/free

Maybe it's best to use normal constructor/destructor with some 
arena memory allocator that never actually frees anything? Must 
be a common problem, anyone got experience with a good design?
Jan 16 2021
parent rikki cattermole <rikki cattermole.co.nz> writes:
What you are describing sounds like regular reference counting.

All resources (i.e. windows) are pinned somewhere in memory. Its just 
that you have to use special API's to destroy them rather than free.
Jan 16 2021