Skip to content

Commit c3ae3a1

Browse files
committed
Select an empty console when dropping the selected console
1 parent 9a45f57 commit c3ae3a1

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

ctru-rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ name = "ctru"
1212

1313
[dependencies]
1414
ctru-sys = { path = "../ctru-sys", version = "0.4" }
15+
const-zero = "0.1.0"
1516
linker-fix-3ds = { git = "https://github.com/Meziu/rust-linker-fix-3ds.git" }
1617
pthread-3ds = { git = "https://github.com/Meziu/pthread-3ds.git" }
1718
libc = { git = "https://github.com/Meziu/libc.git" }

ctru-rs/src/console.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,23 @@ impl Default for Console {
5353

5454
impl Drop for Console {
5555
fn drop(&mut self) {
56+
static mut EMPTY_CONSOLE: PrintConsole = unsafe { const_zero::const_zero!(PrintConsole) };
57+
5658
unsafe {
5759
// Safety: We are about to deallocate the PrintConsole data pointed
5860
// to by libctru. Without this drop code libctru would have a
5961
// dangling pointer that it writes to on every print. To prevent
60-
// this we replace the console with the default if it was selected.
62+
// this we replace the console with an empty one if it was selected.
63+
// This is the same state that libctru starts up in, before
64+
// initializing a console. Writes to the console will not show up on
65+
// the screen, but it won't crash either.
6166

62-
// Get the current console by replacing it with the default.
63-
let default_console = ctru_sys::consoleGetDefault();
64-
let current_console = ctru_sys::consoleSelect(default_console);
67+
// Get the current console by replacing it with an empty one.
68+
let current_console = ctru_sys::consoleSelect(&mut EMPTY_CONSOLE);
6569

6670
if std::ptr::eq(current_console, &*self.context) {
6771
// Console dropped while selected. We just replaced it with the
68-
// default so make sure it's initialized.
69-
if !(*default_console).consoleInitialised {
70-
ctru_sys::consoleInit(Screen::Top.into(), default_console);
71-
}
72+
// empty console so nothing more to do.
7273
} else {
7374
// Console dropped while a different console was selected. Put back
7475
// the console that was selected.

0 commit comments

Comments
 (0)