I'm building a Rust to python FFI using libc
and I am encountering a "invalid pointer" error. On the python side of things, I'm doing:
print(CDLL("./libshared.so").print_stuff(c_char_p("test".encode("utf-8")), 4))
...which does print the intended value but then errors with:
munmap_chunk(): invalid pointer
Fatal Python error: Aborted
Current thread 0x00007eff895ee740 (most recent call first):
File <FILE>, line 7 in <module>
Aborted
I'm assuming that this is occurring because a pointer has been freed by Rust, but C does not know that. Hence, I'm trying to free this pointer by using libc::free
. In order to do this, I need to somehow convert the String
type to *mut _
that the method can receive as an argument. I've tried coersion and transmutation, but neither would be possible. Below is my Rust code.
unsafe {
let string = String::from_raw_parts(array as *mut u8, length, 5);
println!("{:?}", string);
libc::free(/* variable string somehow converted to type c_void **/);
};