PowerToys icon indicating copy to clipboard operation
PowerToys copied to clipboard

Correct StringFormat initialization in test code

Open HackingRepo opened this issue 3 months ago • 1 comments

This PR fixes a memory leak in the DrawIndex function where a Gdiplus::StringFormat object was allocated on the heap using new but never released. Each call to DrawIndex leaked one StringFormat instance, which could accumulate over time and degrade performance.

Changes Replaced heap allocation (new Gdiplus::StringFormat()) with stack allocation (Gdiplus::StringFormat stringFormat;).

Ensured StringFormat is properly destroyed when DrawIndex returns, eliminating the leak.

No functional changes to rendering behavior text alignment and line alignment remain identical.

Impact Bug fix: Prevents memory leaks during repeated calls to DrawIndex.

Performance improvement: Reduces unnecessary memory consumption in scenarios where indexes are drawn frequently.

Code quality: Aligns with RAII best practices in C++ by avoiding manual memory management.

Testing Verified that text rendering output remains unchanged.

Confirmed via runtime checks that no additional allocations persist after DrawIndex completes.

HackingRepo avatar Jan 10 '26 18:01 HackingRepo

Is there an issue for this? Was there any discussion or agreement for this change?

Jay-o-Way avatar Jan 11 '26 13:01 Jay-o-Way

This seems to be a proof-of-concept tool to test the zone rendering of Fancy Zones. It is not compiled as part of the released version of PowerToys, and any potential GDI+ object leaks would be cleared up on exiting the application.

daverayment avatar Jan 13 '26 01:01 daverayment