If I build a debug release of my rust library, everything works perfectly fine from Haskell.
*Lib> getOokle
(3,8)
But if I link to a release build it doesn't .
*Lib> getOokle
(140063742853056,0)
I am clearly doing something wrong, but I'm not really sure what. Also I don't quite understand what the debug and release builds do differently that would produce the different results.
To free the resource, you could write this rust function:
#[no_mangle]
pub extern "C" fn free_ookle(p: *const libc::c_void) {
let b = unsafe { Box::from_raw(p as *mut COokle) };
mem::drop(b); // THis is optional (Box::drop) is called automatically when resource goes out of scope
}
Also, you probably should not cast everything into void pointers. I don`t have a clue, how haskell handles FFI, but I am pretty sure it can interact with properly typed arguments in C functions, so you should be fine using * mut COokle as your argument type.
Maybe also consider not using pointers / heap at all. It might just work returning the COokle directly (if haskell can manage, again no idea if it can). COokle is a pretty simple POD type, and you don`t really gain anything by boxing it, except when haskell can only work with FFI pointer like objects.
Yes, it seems to work fine returning *const COokle so the void cast is uneccessary.
In reality I am returning more complicated structures, structs containing vectors of structs containing strings and the like, but that all works fine as long as I box the return.
Now on to look into freeing it all successfully. Many thanks for the pointer!