News for hackers. Stuff that matters.
Re:On the impossibility of composing finalizers and FFI by cmrx64 on Tuesday February 27, 2024 @11:53AM (#39526332)
I’m deeply familiar with this technique, have used it plenty, have encountered the perils, and so I do not really understand why you think it works well on practice.

It works well in only the case where you have perfectly well scoped small regions that you can model as your lambda. When you need to actually do anything intricate with the lifetime where you want it to escape (probably into a datastructure), the callback won’t cut it and it’s on you to ensure the foreignptr’s lifetime becomes interlinked with the returned b

Re:On the impossibility of composing finalizers and FFI by nh2 on Sunday March 10, 2024 @01:30PM (#39660873)
I don't quite follow the argument:

> it’s on you to ensure the foreignptr’s lifetime becomes interlinked with the returned b

Yes; the simplest way to do that is to make sure that your data types never contain raw `Ptr`, only `ForeignPtr` -- same as in C++, where seeing `mytype * x` should ring the alarm bells.

You could say "but what if I call another FFI function that needs the Ptr as an argument"? In that case, surely the function needs to document if it takes ownership of the pointer. If it doesn't document that, yes, it'll crash; but that's unrelated to finalizers (the "impossibility of composing" of which which is what the post claimed); it would also crash if no finalizers were involved.