From 60809b7884621cb949eec3ddbe1eae76103d17c2 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 6 Oct 2018 00:32:28 +0200 Subject: c_parse_args(): Box the returned `Config` pointer Otherwise it gets freed too early by Rust. Add (re-add) a free function for it too now that we know it must be freed manually. --- dome_key_map.h | 2 ++ src/cocoa_bridge.rs | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/dome_key_map.h b/dome_key_map.h index 8701b4a..8323fdd 100644 --- a/dome_key_map.h +++ b/dome_key_map.h @@ -37,6 +37,8 @@ const Config *c_parse_args(const char *const *args, size_t length); void c_run_key_action(State *state, Trigger trigger, const Trigger *mode); +void config_free(Config *ptr); + void logger_init(void); void state_free(State *ptr); diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index 2ef4979..70d837a 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -322,7 +322,13 @@ pub extern "C" fn c_parse_args( let config = config::parse_args(&args); - &config as *const Config + Box::into_raw(Box::new(config)) +} + +#[no_mangle] +pub extern "C" fn config_free(ptr: *mut Config) { + if ptr.is_null() { return } + unsafe { Box::from_raw(ptr); } } -- cgit v1.2.3