Skip to content

Commit

Permalink
Use NativeWindow::clone_from_ptr() to avoid Segfault
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rib committed Aug 12, 2022
1 parent 4818de6 commit 93828a1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion android-activity/src/game_activity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { .. } => {
Expand Down
5 changes: 4 additions & 1 deletion android-activity/src/native_activity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
}
Expand Down

0 comments on commit 93828a1

Please sign in to comment.