diff options
| author | Teddy Wing | 2018-09-04 16:15:14 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-09-06 04:03:20 +0200 |
| commit | ce1eca32e2249a72692c6eb01052361d98af29c3 (patch) | |
| tree | b24d1c6f68d72664e31d6a8952b3ae83fdc1ed2e | |
| parent | 84d29715900209b8ab4c600ad40ef99cdad03bd6 (diff) | |
| download | dome-key-map-ce1eca32e2249a72692c6eb01052361d98af29c3.tar.bz2 | |
c_run_key_action(): Fix segfault on `in_mode`
Finally figured out where the problem was with the `in_mode` on my
`CKeyActionResult` getting freed too early.
It happens because we're trying to use the reference to the `in_mode`
slice from the `KeyActionResult` returned by
`run_key_action_for_mode()`. We were putting that reference in the
`in_mode` of the `CKeyActionResult` returned by `c_run_key_action()`,
but it would get freed in that function.
In order to make it last indefinitely, we needed to convert it into a
raw boxed pointer to prevent Rust from deallocating it. This does mean
we now have a memory leak, but we'll just have to make a call into Rust
to free it from the C code.
| -rw-r--r-- | moder.c | 3 | ||||
| -rw-r--r-- | src/cocoa_bridge.rs | 13 |
2 files changed, 10 insertions, 6 deletions
@@ -10,7 +10,7 @@ int main() { .length = SIZE }; const CKeyActionResult *mode = c_run_key_action(mode_trigger, NULL); - /* printf("%d\n", *mode->kind); */ + printf("%d\n", *mode->kind); HeadphoneButton buttons[] = {HeadphoneButton_Down}; Trigger trigger = { @@ -21,6 +21,7 @@ int main() { printf("%d\n", *result->kind); printf("%s", result->action); + /* printf("\n%d\n", *mode->kind); */ return 0; } diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index 8581b46..e860d24 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -107,7 +107,7 @@ pub extern "C" fn c_run_key_action( if mode.is_null() { None } else { - println!("{:?}", *mode); + println!("In mode(110): {:?}", *mode); assert!(!(*mode).buttons.is_null()); Some( @@ -115,7 +115,7 @@ pub extern "C" fn c_run_key_action( ) } }; - println!("{:?}", mode); + println!("Mode after unsafe (118): {:?}", mode); let result = run_key_action_for_mode(trigger, mode); let result = match result { @@ -138,12 +138,13 @@ pub extern "C" fn c_run_key_action( // ); let trigger; let in_mode = if let Some(m) = k.in_mode { - trigger = Trigger { + let boink = Trigger { buttons: m.as_ptr(), length: m.len(), }; - &trigger + trigger = Box::into_raw(Box::new(boink)); // TODO: memory leak + trigger } else { ptr::null() }; @@ -158,7 +159,7 @@ pub extern "C" fn c_run_key_action( kind: &k.kind, in_mode: in_mode, }; - println!("{:?}", result); + println!("CKeyActionResult(161): {:?}", result); // mem::forget(result); result }, @@ -172,8 +173,10 @@ pub extern "C" fn c_run_key_action( }; // println!("hey result: {:?}", result); // mem::forget(result); + println!("Result 177: {:?}", result); let r = Box::new(result); let r2 = Box::into_raw(r); + println!("r2: {:?}", r2); // &result as *const CKeyActionResult r2 as *const CKeyActionResult |
