Fix GDI resource leaks and visual artifacts in VwDrawRootBuffered#557
Merged
johnml1135 merged 5 commits intospec-kit-rebasefrom Nov 22, 2025
Merged
Fix GDI resource leaks and visual artifacts in VwDrawRootBuffered#557johnml1135 merged 5 commits intospec-kit-rebasefrom
johnml1135 merged 5 commits intospec-kit-rebasefrom
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>
Copilot
AI
changed the title
[WIP] Re-implement buffering code for views project
Fix GDI resource leaks and visual artifacts in VwDrawRootBuffered
Nov 22, 2025
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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