-
-
Notifications
You must be signed in to change notification settings - Fork 41
Fix GDI resource leaks and visual artifacts in VwDrawRootBuffered #557
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
Fix GDI resource leaks and visual artifacts in VwDrawRootBuffered #557
Conversation
- Fixed DrawTheRoot to properly manage GDI bitmap resources * Clean up previous cached bitmap/DC before creating new ones * Keep bitmap in m_hdcMem for ReDrawLastDraw support * Delete stock bitmap that comes with new DC (not the previous bitmap) - Fixed DrawTheRootRotated to use local DC/bitmap * Uses local resources since rotation makes caching impractical * Properly cleanup bitmap and DC on exit and in exception paths The previous code had a critical bug: it was deleting the OLD bitmap (hbmpOld) immediately after selecting the new one, instead of keeping it for restoration. This caused resource leaks and visual artifacts. The new implementation follows the correct GDI resource management pattern: 1. Create compatible DC and bitmap 2. Select bitmap into DC (save old bitmap) 3. Delete the stock bitmap (not the old one!) 4. Draw to bitmap 5. Blit to screen 6. Keep bitmap/DC cached in m_hdcMem for ReDrawLastDraw (DrawTheRoot) OR cleanup local resources (DrawTheRootRotated, DrawTheRootAt) Co-authored-by: johnml1135 <13733556+johnml1135@users.noreply.github.com>
Co-authored-by: johnml1135 <13733556+johnml1135@users.noreply.github.com>
The previous fix incorrectly deleted the stock bitmap returned by SelectObjectBitmap. Stock GDI objects should not be deleted by applications. Changes: - DrawTheRoot: Leave custom bitmap selected in m_hdcMem for caching (it gets deleted on next draw or in destructor) - DrawTheRootRotated: Restore stock bitmap before cleanup, then delete custom bitmap and DC - Updated documentation to explain correct stock bitmap handling The correct pattern for local DCs (DrawTheRootRotated, DrawTheRootAt): 1. Create DC with CreateCompatibleDC (has stock bitmap) 2. Create custom bitmap and select it (saves stock bitmap) 3. Draw to custom bitmap 4. Blit to screen 5. Restore stock bitmap into DC 6. Delete custom bitmap 7. Delete DC The correct pattern for cached DCs (DrawTheRoot): 1. Clean up previous cached custom bitmap if any 2. Create DC with CreateCompatibleDC 3. Create custom bitmap and select it (stock bitmap is returned but not deleted) 4. Draw to custom bitmap 5. Blit to screen 6. Keep DC and custom bitmap for ReDrawLastDraw This ensures proper GDI resource management without attempting to delete stock objects, which can cause issues in debug builds. Co-authored-by: johnml1135 <13733556+johnml1135@users.noreply.github.com>
Co-authored-by: johnml1135 <13733556+johnml1135@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes critical GDI bitmap management bugs in VwDrawRootBuffered that were causing resource leaks and visual artifacts. The root cause was incorrect handling of stock vs. custom GDI bitmaps - the code was deleting stock bitmaps (which should never be deleted) and leaking custom bitmaps (which should be deleted when no longer needed).
Key changes:
- Fixed
DrawTheRootto properly cache custom bitmap inm_hdcMemwithout deleting stock bitmap - Fixed
DrawTheRootRotatedto use local DC/bitmap with proper cleanup and stock bitmap restoration - Added exception handling cleanup for
DrawTheRootRotatedto prevent resource leaks
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| Src/views/VwRootBox.cpp | Fixed GDI resource management in DrawTheRoot (cached DC pattern) and DrawTheRootRotated (local DC pattern) methods to properly handle stock vs. custom bitmaps |
| IMPLEMENTATION_SUMMARY.md | Added comprehensive technical documentation explaining the bug, fix, architecture decisions, and testing strategy |
| BUFFERING_FIX.md | Added detailed explanation of the root cause, solution patterns, and key principles for GDI bitmap handling |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
Fixed critical GDI bitmap management bugs in
VwDrawRootBufferedcausing resource leaks and visual artifacts. The code was deleting stock bitmaps (which should never be deleted) and leaking custom bitmaps (which should always be deleted).Root cause: After
SelectObjectBitmap(dc, newBitmap), the code deleted the returned stock bitmap instead of the created custom bitmap:Changes
DrawTheRoot(cached DC pattern for ReDrawLastDraw):m_hdcMemfor cachingDrawTheRootRotated(local DC pattern):Pattern difference:
DrawTheRootcaches the DC/bitmap forReDrawLastDrawoptimization (used when form is disabled).DrawTheRootRotateduses local resources since rotation complicates caching.CI-ready checklist
.github/commit-guidelines.md(subject ≤ 72 chars, no trailing punctuation; if body present, blank line then ≤ 80-char lines).Src/**folders touched, correspondingCOPILOT.mdfiles are updated or explicitly confirmed still accurate.Notes for reviewers
Testing focus:
ReDrawLastDraw)Technical context: Stock GDI objects (the default 1x1 bitmap in a new DC) must never be deleted per Windows GDI guidelines. The destructor already properly cleaned up cached bitmaps.
Documentation:
BUFFERING_FIX.mdandIMPLEMENTATION_SUMMARY.mdadded for technical reference.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
This change is