Fix memory leak with Epub object getting orphaned

This commit is contained in:
Dave Allie 2025-12-06 02:49:10 +11:00
parent 7198d943b0
commit 98c8e7e77c
No known key found for this signature in database
GPG Key ID: F2FDDB3AD8D0276F
4 changed files with 22 additions and 10 deletions

View File

@ -51,7 +51,7 @@ Epub* loadEpub(const std::string& path) {
}
Serial.println("Failed to load epub");
free(epub);
delete epub;
return nullptr;
}
@ -122,6 +122,8 @@ void onSelectEpubFile(const std::string& path) {
} else {
exitScreen();
enterNewScreen(new FullScreenMessageScreen(renderer, "Failed to load epub", REGULAR, false, false));
delay(2000);
onGoHome();
}
}

View File

@ -14,6 +14,10 @@ void EpubReaderScreen::taskTrampoline(void* param) {
}
void EpubReaderScreen::onEnter() {
if (!epub) {
return;
}
sectionMutex = xSemaphoreCreateMutex();
epub->setupCacheDir();
@ -42,10 +46,16 @@ void EpubReaderScreen::onEnter() {
void EpubReaderScreen::onExit() {
xSemaphoreTake(sectionMutex, portMAX_DELAY);
vTaskDelete(displayTaskHandle);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(sectionMutex);
displayTaskHandle = nullptr;
sectionMutex = nullptr;
delete section;
section = nullptr;
delete epub;
epub = nullptr;
}
void EpubReaderScreen::handleInput(const Input input) {
@ -141,7 +151,7 @@ void EpubReaderScreen::renderPage() {
section->setupCacheDir();
if (!section->persistPageDataToSD()) {
Serial.println("Failed to persist page data to SD");
free(section);
delete section;
section = nullptr;
xSemaphoreGive(sectionMutex);
return;

View File

@ -26,10 +26,6 @@ class EpubReaderScreen final : public Screen {
public:
explicit EpubReaderScreen(EpdRenderer* renderer, Epub* epub, const std::function<void()>& onGoHome)
: Screen(renderer), epub(epub), onGoHome(onGoHome) {}
~EpubReaderScreen() override {
free(section);
free(epub);
}
void onEnter() override;
void onExit() override;
void handleInput(Input input) override;

View File

@ -32,6 +32,7 @@ void FileSelectionScreen::loadFiles() {
void FileSelectionScreen::onEnter() {
basepath = "/";
loadFiles();
selectorIndex = 0;
// Trigger first update
updateRequired = true;
@ -45,8 +46,11 @@ void FileSelectionScreen::onEnter() {
}
void FileSelectionScreen::onExit() {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
files.clear();
}
void FileSelectionScreen::handleInput(const Input input) {