fix: Close leaked file descriptors in SleepActivity and web server (#869)
## Summary - **SleepActivity.cpp**: Add missing `file.close()` calls in 3 code paths that open BMP files for sleep screen rendering but never close them before returning. Affects random custom sleep images, the `/sleep.bmp` fallback, and book cover sleep screens. - **CrossPointWebServer.cpp**: Add missing `dir.close()` in the delete handler when `Storage.open()` returns a valid `FsFile` that is not a directory. ## Context SdFat is configured with `DESTRUCTOR_CLOSES_FILE=0`, which means `FsFile` objects are **not** automatically closed when they go out of scope. Every opened file must be explicitly closed. The SleepActivity leaks are particularly impactful because they occur on every sleep cycle. While ESP32 deep sleep clears RAM on wake, these leaks can still affect the current session if sleep screen rendering is triggered multiple times (e.g., cover preview, or if deep sleep fails to engage). The web server leak in `handleDelete()` is a minor edge case (directory path that opens successfully but `isDirectory()` returns false), but it's still worth fixing for correctness. ## Test plan - [x] Verify sleep screen still renders correctly (custom BMP, fallback, cover modes) - [x] Verify folder deletion still works via the web UI - [ ] Monitor free heap before/after sleep screen rendering to confirm no leak 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Jan Bažant <janbazant@Jan--Mac-mini.local> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Dave Allie <dave@daveallie.com>
This commit is contained in:
@@ -82,9 +82,11 @@ void SleepActivity::renderCustomSleepScreen() const {
|
||||
Bitmap bitmap(file, true);
|
||||
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
||||
renderBitmapSleepScreen(bitmap);
|
||||
file.close();
|
||||
dir.close();
|
||||
return;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,8 +100,10 @@ void SleepActivity::renderCustomSleepScreen() const {
|
||||
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
||||
LOG_DBG("SLP", "Loading: /sleep.bmp");
|
||||
renderBitmapSleepScreen(bitmap);
|
||||
file.close();
|
||||
return;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
renderDefaultSleepScreen();
|
||||
@@ -267,8 +271,10 @@ void SleepActivity::renderCoverSleepScreen() const {
|
||||
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
||||
LOG_DBG("SLP", "Rendering sleep cover: %s", coverBmpPath.c_str());
|
||||
renderBitmapSleepScreen(bitmap);
|
||||
file.close();
|
||||
return;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
return (this->*renderNoCoverSleepScreen)();
|
||||
|
||||
Reference in New Issue
Block a user