News for hackers. Stuff that matters.
Re:Rust FFI: Sending strings to the outside world by thayne on Thursday May 18, 2017 @12:21AM (#14364788)
It might be good to mention how to free the memory. By passing the pointer back to a rust function like

  #[no_mangle]
  pub extern fn free_string(p: *mut c_char) {
    unsafe {
      let _ = CString::from_raw(p);
      // optionally you could explicitly call drop, but
      // that is unnecessary, since it is automatically dropped
      // at end of scope
    }
  }

Also the `into_raw` method on CString gives you a pointer while simultaneously taking ownership of the CString, so mem::forget isn't necessary if you use `into_raw` rather than `as_ptr`.

There are no comments on this comment yet.