From 97a19adb5a2e53a86ba5505795f505117d2fa7b6 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 26 Aug 2018 13:30:09 +0200 Subject: c_run_key_action(): Correctly send action string to C Using `.as_ptr()` on the `CString` didn't work. The C code wouldn't print anything. Turns out I needed to use `.into_raw()` in order to: > [Consume] the CString and [transfer] ownership of the string to a C > caller. (https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_raw) This allows us to correctly access and print the string in our C code. Note that we'll still need to free the string using a new Rust function that should be callable from C, otherwise we'll end up with a memory leak. --- src/cocoa_bridge.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index f33a739..49fc9f2 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -69,7 +69,7 @@ pub extern "C" fn c_run_key_action( match k.action { Some(a) => { CKeyActionResult { - action: CStr::from_bytes_with_nul(b"test?\n\0").unwrap().as_ptr(), + action: a.into_raw(), // memory leak, must be freed from Rust kind: &k.kind, } }, -- cgit v1.2.3