C binding does not seem to be usable. #681
Replies: 2 comments 3 replies
-
See the manifoldc tests for usage examples. The Manifold library returns fresh For an example of using type t = C.Types.CrossSection.t Ctypes_static.ptr
let size = C.Funcs.cross_section_size () |> size_to_int
let destruct t = C.Funcs.destruct_cross_section t
let alloc () =
let finalise = Mem.finaliser C.Types.CrossSection.t destruct in
let buf = Mem.allocate_buf ~finalise size in
( buf
, Ctypes.coerce Ctypes_static.(ptr void) Ctypes_static.(ptr C.Types.CrossSection.t) buf
)
let circle ?(fn = 0) rad =
let buf, t = alloc () in
let _ = C.Funcs.cross_section_circle buf rad fn in
t |
Beta Was this translation helpful? Give feedback.
-
Just stumbled upon something similar while building rust bindings for this project. I'm no professional when it comes to C/C++ or developing bindings for these in rust and I wonder how this (de)allocation really is supposed to work. From my understanding with the current pattern we got all the stuff where we know the size at compile time in memory provided by the user, but the vectors in the memory allocated by Manifold, and for releasing the memory we also call a function in Manifold, where I expect we call the release function on the user side, as this is the one which allocated the memory in the first place. So far I tried some things to rectify this a bit, but everything except this weird pattern leaks memory. Only the current version, where memory is allocated by the user side and deleted on the Manifold side does not leak memory... and I can't understand why that is. One thing I tried is adding an allocation function to allocate the memory in Manifold too, as it is already the case for deletion: ManifoldManifold* manifold_alloc_manifold() { return to_c(new Manifold); } But calling this allocation function and later the Any ideas why that might be? |
Beta Was this translation helpful? Give feedback.
-
It's true for many of the calls in the C binding, but here's the one I hit first:
How is a caller (outside of C++) supposed to know what size "mem" should be?
The call seems to be trying to copy the CrossSection into caller provided memory?
Why is the copy necessary?
Beta Was this translation helpful? Give feedback.
All reactions