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.
This commit is contained in:
Robert Bragg
2022-08-12 18:45:15 +01:00
parent 4818de6709
commit 93828a18a9
2 changed files with 8 additions and 2 deletions
+4 -1
View File
@@ -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 { .. } => {
+4 -1
View File
@@ -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(),
));
}