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

Steven Schveighoffer schveiguy at gmail.com
Wed Jul 8 16:28:02 UTC 2020


On 7/8/20 11:27 AM, Avrina wrote:
> 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.
>>
> 
> 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.

Oh yeah, I misunderstood the problem.

That's going to be the case no matter what. The only true solution would 
be to use algorithms that use RefCounted instead of the GC. Which may be 
trading one problem for another.

-Steve


More information about the Digitalmars-d mailing list