non-determinant object lifetime and memory management

Frustrated c1514843 at drdrb.com
Sat Nov 30 17:49:10 PST 2013


On Saturday, 30 November 2013 at 12:51:46 UTC, Rene Zwanenburg 
wrote:
> On Saturday, 30 November 2013 at 08:35:23 UTC, Frustrated wrote:
>> I need to pass around some objects(specifically int[]) that 
>> may be used by several other objects at the same time. While I 
>> could clone these and free them when the parent object is done 
>> this wastes memory for no real reason except ease of use.
>>
>> Since many objects may contain a ptr to the array, what would 
>> be the best way to deal with deallocating them? I could wrap 
>> the array in a collection an use ARC but is there a better way?
>>
>> Is there something in std.allocators that can help?
>>
>> (Should be obvious that I'm trying to avoid the GC)
>
> You can use the Array type in std.container [1]. It uses ref 
> counting and the C heap internally.
>
> [1] http://dlang.org/phobos/std_container.html#.Array

How does it work? When you call clear it decrements the reference 
count and at 0 it free's the memory?

That is the only self managed container in std.container?


More information about the Digitalmars-d-learn mailing list