News for hackers. Stuff that matters.
Re:On the impossibility of composing finalizers and FFI by celeritascelery on Monday February 26, 2024 @12:10PM (#39513853)
Given how much research there is into this topic, I am sure that I just don't understand the complexity of it. But to me it seems like you could have a function that associates one value with another in the GC. Something like `gc_borrows_from`. You would than write the problematic code like this:

    function blob_contents(blob)
       local len_out = ffi.new('unsigned int')
       local contents = gc_borrows_from(blob, hb.hb_blob_get_data(blob, len_out))
       local len = len_out[0];
       return ffi.string(contents, len)
    end
This would tell the GC that the data returned by `hb_blob_get_data` is borrowed from blob, and it can't collect `blob` until `contents` is also unreachable. How to implement that would be up to the runtime, but it seems reasonable to have a wrapper type that holds a traceable reference back to blob.
Re:On the impossibility of composing finalizers and FFI by kevingadd on Tuesday February 27, 2024 @04:39AM (#39522075)
.NET calls this concept a 'dependent handle' if I understand you correctly, though allowing direct use of it is somewhat new:

https://learn.microsoft.com/en-us/dotnet/api/system.runtime....

And there's an older API that wraps them which I've found quite handy: https://learn.microsoft.com/en-us/dotnet/api/system.runtime....

Essentially, if you want to (externally - in code you wrote) associate two objects with each other without being able to modify the code of either of those objects, dependent handles are the ticket. It only creates a one-way liveness relationship, though, which may not be sufficient for every use case...

Re:On the impossibility of composing finalizers and FFI by planede on Tuesday February 27, 2024 @04:56AM (#39522194)
In C++ that would be an "aliasing shared_ptr", for a reference counting example of the same idea.
Re:On the impossibility of composing finalizers and FFI by pizlonator on Tuesday February 27, 2024 @09:55AM (#39524820)
Cute, but not a complete solution.

Eventually you’ll want an object graph cycle that traverses through the non-GC heap and then you’re really screwed.