Ever since I first studied system programming, it's not easy for me to wrap my head around rules. Now I'm confused about a memory leak. Consider an example. Let's say Rust casts a pointer (to a string) that Python will catch.
In Rust, (I just send a CString pointer)
use std::ffi::CString; pub extern fn do_something() -> *const c_char { CString::new(some_string).unwrap().as_ptr() }
In Python (I dereference a pointer)
def call_rust(): lib = ctypes.cdll.LoadLibrary(rustLib) lib.do_something.restype = ctypes.c_void_p c_pointer = lib.do_something() some_string = ctypes.c_char_p(c_pointer).value
Now, my question is to free memory. I thought this should be freed in Python, but then ownership comes up. Because as_ptr seems to have an immutable link. So, I am confused about whether I should free memory in Rust or Python (or both?). If it is Rust, then how should I release it when the control flow returns to Python?
python memory-leaks rust ctypes ffi
Waffle's crazy peanut
source share