Skip to content

Add back Console::drop #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ctru-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ name = "ctru"

[dependencies]
ctru-sys = { path = "../ctru-sys", version = "0.4" }
const-zero = "0.1.0"
linker-fix-3ds = { git = "https://github.com/Meziu/rust-linker-fix-3ds.git" }
pthread-3ds = { git = "https://github.com/Meziu/pthread-3ds.git" }
libc = { git = "https://github.com/Meziu/libc.git" }
Expand Down
28 changes: 28 additions & 0 deletions ctru-rs/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,31 @@ impl Default for Console {
Console::init(Screen::Top)
}
}

impl Drop for Console {
fn drop(&mut self) {
static mut EMPTY_CONSOLE: PrintConsole = unsafe { const_zero::const_zero!(PrintConsole) };

unsafe {
// Safety: We are about to deallocate the PrintConsole data pointed
// to by libctru. Without this drop code libctru would have a
// dangling pointer that it writes to on every print. To prevent
// this we replace the console with an empty one if it was selected.
// This is the same state that libctru starts up in, before
// initializing a console. Writes to the console will not show up on
// the screen, but it won't crash either.

// Get the current console by replacing it with an empty one.
let current_console = ctru_sys::consoleSelect(&mut EMPTY_CONSOLE);

if std::ptr::eq(current_console, &*self.context) {
// Console dropped while selected. We just replaced it with the
// empty console so nothing more to do.
} else {
// Console dropped while a different console was selected. Put back
// the console that was selected.
ctru_sys::consoleSelect(current_console);
}
}
}
}