how do you mix ref counted types and non- at nogc algorithms

Avrina avrina12309412342 at gmail.com
Wed Jul 8 15:27:09 UTC 2020


On Wednesday, 8 July 2020 at 12:30:09 UTC, Steven Schveighoffer 
wrote:
> On 7/8/20 7:26 AM, ikod wrote:
>> Hello,
>> 
>> I have ref-counted type (keeps reference to mallocated memory) 
>> which I would like to release as soon as possible (when I have 
>> no refs from my code).
>> 
>> The problem is - standard algorithms can use some GC 
>> allocations to keep refs to my data until GC is collected, so 
>> that I have no control over memory consumption when non- at nogc 
>> algorithms applied to RC data.
>> 
>> Right now I have to avoid something like my_data.splitOn(), 
>> but is there any better solution?
>
> My solution to this was to use the GC for ref counted data.
>
> See here: 
> http://schveiguy.github.io/iopipe/iopipe/refc/RefCounted.html
>
> This way, the resources represented by the type (generally 
> things like files or malloc'd items) are cleaned up, but the 
> memory is still safe to use.
>
> -Steve

My understanding to what he means is that a copy of RefCounted 
will be kept in GC'd memory. So the counter won't hit 0 until the 
GC collects and calls the destructor for that memory, which isn't 
guarantee'd to ever happen. Your implementation would have the 
same problem.


More information about the Digitalmars-d mailing list