From 63b26435340e2c16c239a87e8f214d9a5a253a65 Mon Sep 17 00:00:00 2001 From: Uri Tauber <142022451+Uri-Tauber@users.noreply.github.com> Date: Fri, 20 Feb 2026 02:08:37 +0200 Subject: [PATCH] fix: Fix dangling pointer (#1010) ## Summary * **What is the goal of this PR?** Small fix for bug I found. ## Additional Context 1. `RecentBooksActivity::loop()` calls `onSelectBook(recentBooks[selectorIndex].path)` - passing a **reference** to the path 2. `onSelectBook` is `onGoToReader` which first calls `exitActivity()` 3. `exitActivity()` triggers `RecentBooksActivity::onExit()` which call `recentBooks.clear()` 4. The string reference `initialEpubPath` is now a **dangling reference** - the underlying string has been destroyed 5. When the reference is then used in `new ReaderActivity(...)`, it reads garbage memory 6. The same issue occurs in `HomeActivity` at line 200 with the same pattern The fix is to make a copy of the string in `onGoToReader` before calling `exitActivity()`, so the path data persists even after the activity clears its data structures. --- ### AI Usage Did you use AI tools to help write this code? _**< YES >**_ Claude found the bug, after I shared with it a serial log. --- src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2fa20bed..6b227065 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -215,9 +215,9 @@ void onGoHome(); void onGoToMyLibraryWithPath(const std::string& path); void onGoToRecentBooks(); void onGoToReader(const std::string& initialEpubPath) { + const std::string bookPath = initialEpubPath; // Copy before exitActivity() invalidates the reference exitActivity(); - enterNewActivity( - new ReaderActivity(renderer, mappedInputManager, initialEpubPath, onGoHome, onGoToMyLibraryWithPath)); + enterNewActivity(new ReaderActivity(renderer, mappedInputManager, bookPath, onGoHome, onGoToMyLibraryWithPath)); } void onGoToFileTransfer() {