From 93828a18a9be395d7bd9bb660f4926f38b5fe85a Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Fri, 12 Aug 2022 18:45:15 +0100 Subject: [PATCH] Use NativeWindow::clone_from_ptr() to avoid Segfault Since f24606cc84 in android-ndk-rs then NativeWindow now implements Clone and Drop which was technically a breaking change since it changed the ownership contract for existing users of NativeWindow::from_ptr(). We now use NativeWindow::clone_from_ptr() to account for the fact that the window will be unconditionally _released() when NativeWindow gets dropped. This addresses a crash I was debugging with the Cpal and Oboe examples which turned out to be nothing to do with the examples themselves. --- android-activity/src/game_activity/mod.rs | 5 ++++- android-activity/src/native_activity/mod.rs | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/android-activity/src/game_activity/mod.rs b/android-activity/src/game_activity/mod.rs index 8453ea3..3f36666 100644 --- a/android-activity/src/game_activity/mod.rs +++ b/android-activity/src/game_activity/mod.rs @@ -269,8 +269,11 @@ impl AndroidAppInner { } MainEvent::InitWindow { .. } => { let win_ptr = (*app_ptr.as_ptr()).window; + // It's important that we use ::clone_from_ptr() here + // because NativeWindow has a Drop implementation that + // will unconditionally _release() the native window *self.native_window.write().unwrap() = Some( - NativeWindow::from_ptr(NonNull::new(win_ptr).unwrap()), + NativeWindow::clone_from_ptr(NonNull::new(win_ptr).unwrap()), ); } MainEvent::TerminateWindow { .. } => { diff --git a/android-activity/src/native_activity/mod.rs b/android-activity/src/native_activity/mod.rs index 27d5c78..817328a 100644 --- a/android-activity/src/native_activity/mod.rs +++ b/android-activity/src/native_activity/mod.rs @@ -271,8 +271,11 @@ impl AndroidAppInner { } MainEvent::InitWindow { .. } => { let win_ptr = (*app_ptr.as_ptr()).window; + // It's important that we use ::clone_from_ptr() here + // because NativeWindow has a Drop implementation that + // will unconditionally _release() the native window *self.native_window.write().unwrap() = - Some(NativeWindow::from_ptr( + Some(NativeWindow::clone_from_ptr( NonNull::new(win_ptr).unwrap(), )); }