Do you want to work on this issue?
You can request for a bounty in order to promote it!
Safer context for closures #125
kornelski posted onGitHub
Currently gifski C API takes a callback function + a context pointer. From Swift's perspective this has to be a plain, context-free function, and the context pointer is meaningless (unsafe unretained). That makes it awkward to use (doesn't allow normal closures), and is unsafe.
Can we do better?
I don't know what's Swift solution to the memory management problem here. In the olden days of manual refcounting, it'd suffice to call an extra [context retain]
before setting up the callback's context, and [context release]
after finishing the operation. Rust's approach to this is into_raw()
and from_raw()
that "leak" memory and "unleak" it later.
The second part of the problem is allowing normal closures, instead of sneaking the context through an unsafe pointer.
Perhaps closures could be cast to an ObjC block? Blocks can be represented as regular pointers. The callback function could be just a small function that calls a block, and the block would be passed through the context pointer.
Or can Swift "box" a closure into a container that can be sent as a single pointer? It could work similar to blocks, but with "native" Swift closure.