TC 9.5b9 - Color-Config GDI Memory leak

Bug reports will be moved here when the described bug has been fixed

Moderators: white, Hacker, petermad, Stefan2

User avatar
HolgerK
Power Member
Power Member
Posts: 5406
Joined: 2006-01-26, 22:15 UTC
Location: Europe, Aachen

TC 9.5b9 - Color-Config GDI Memory leak

Post by *HolgerK »

Steps to reproduce:

- Start "Task Manager"
- Switch to "Details" view
- Add column "GDI Objects"
- Start Total Commander (x86 +x64)
- Open "Color Config" (cm_ColorConfig)
- Close "Color Config"
- Repeat the last two steps multiple time and observe the number of "GDI Objects" in Task Manager.

The number of GDI objects is increased by about 15 to 20 objects for each open/close cycle (W10 1909, TC 9.5b9 dark mode enabled).

Regards
Holger
Make our planet great again
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *ghisler(Author) »

Confirmed for dark mode only!
Author of Total Commander
https://www.ghisler.com
User avatar
Usher
Power Member
Power Member
Posts: 1675
Joined: 2011-03-11, 10:11 UTC

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *Usher »

I suspect that it may be also possible in normal mode when using dark mode preview. Use "Define colors by file type", click "Dark<->Normal" button a few times and "Cancel" or "OK", then repeat actions several times. Unfortunately, I cannot test it in Windows 10 now…
Andrzej P. Wozniak
Polish subforum moderator
User avatar
HolgerK
Power Member
Power Member
Posts: 5406
Joined: 2006-01-26, 22:15 UTC
Location: Europe, Aachen

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *HolgerK »

ghisler(Author) wrote: 2019-12-16, 11:21 UTC Confirmed for dark mode only!
W10 1909, TC9.50b9 x64 dark mode disabled: minimum 1 GDI object per cycle.
Leak is smaller but exist too.
Just tested with TC9.22a x64: same 1 object leak.
So it's not introduced with TC9.50beta.

Regards
Holger
Make our planet great again
User avatar
HolgerK
Power Member
Power Member
Posts: 5406
Joined: 2006-01-26, 22:15 UTC
Location: Europe, Aachen

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *HolgerK »

Usher wrote: 2019-12-16, 15:55 UTC I suspect that it may be also possible in normal mode when using dark mode preview.
Yepp!
S2R: TC9.50b9x64 dark mode disabled:
- cm_ColorConfig
- combo box "Others:"
- change combo box selection multiple time down and up again to toggle the color preview (lister, compare, dark mode,...)
-> about 20 additional GDI objects after closing the dialog

Use "Define colors by file type", click "Dark<->Normal" button a few times and "Cancel" or "OK", then repeat actions several times. Unfortunately, I cannot test it in Windows 10 now…
Did as you suggested with defined color filters, but this seems to be more or less okay. Only 1 or 2 additional objects.

Regards
Holger
Make our planet great again
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *ghisler(Author) »

Just tested with TC9.22a x64: same 1 object leak.
I cannot reproduce that, here it only happens in dark mode (which I managed to fix in the meantime). Maybe it depends on the settings on that page?
Author of Total Commander
https://www.ghisler.com
User avatar
HolgerK
Power Member
Power Member
Posts: 5406
Joined: 2006-01-26, 22:15 UTC
Location: Europe, Aachen

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *HolgerK »

9.22ax64_gdi.ini

Code: Select all

[Configuration]
UseNewDefFont=1
SetEncoding=äö.do.not.remove
firstmnu=2618
FirstTimeIconLib=0
FirstTime=0
test=119
SeparateTree=0
PanelsVertical=0
[FileSystemPlugins64]
$checksum$=3014672
[ButtonbarCache]
IconDll_default.bar=
IconDll_vertical.bar=
[1920x1080 (8x16)]
CmdSelX=636
CmdSelY=329
CmdSelDX=653
CmdSelDY=371
CmdSelMax=0
[Shortcuts]
C=cm_ColorConfig
[left]
path=c:\
ViewMode=10001
activepanelcolor=-1
activepanelcolor2=-1
ShowAllDetails=1
SpecialView=0
show=1
sortorder=0
negative Sortorder=0
[right]
path=c:\
ViewMode=10001
activepanelcolor=-1
activepanelcolor2=-1
ShowAllDetails=1
SpecialView=0
show=1
sortorder=0
negative Sortorder=0
[RightHistory]
0=c:\
[LeftHistory]
0=c:\
Only added a short cut <C> for cm_ColorConfig.

- Start TC with 9.22ax64_gdi.ini
- Press once <C>, <Esc>
- Wait some seconds until task manager displays a stable GDI object count: 373
- repeat 10 times the sequence <C>, <Esc> (delay between the key strokes about 0.5 sec)
- > GDI object count: 383
TC9.22a x64, W10 1909.

Regards
Holger
Make our planet great again
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *ghisler(Author) »

I can reproduce the GDI leak on Windows 10 1809 only, also with older TC versions. It does not occur on Windows 10 1803. It also happens when opening other dialog boxes, e.g. repeat F5 - ESC. Unfortunately it seems to be a bug in Windows 10 itself - I cannot find any leak in my code. Btw, the 32-bit version doesn't seem to be affected.
Author of Total Commander
https://www.ghisler.com
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *ghisler(Author) »

I have tried the tool GDIView from Nirsoft to analyze the leak:
1. Download the tool (64-bit version): https://www.nirsoft.net/utils/gdi_handles.html
2. Start it
3. Run Total Commander 64-bit
4. Opened main settings a few times
5. Pressed F5 in GDIView
6. Chose Options - Reset last counters
7. Opened main settings of Total Commander and closed it
8. Pressed F5 in GDIView
Results:
There are 3 new GDI objects ("All GDI" on the right), but GDIView cannot detect what they are. They are clearly not allocated by my app, but something internal of the window manager to display the dialog:
https://www.ghisler.ch/images/gdiview.png
Any ideas?
Author of Total Commander
https://www.ghisler.com
User avatar
HolgerK
Power Member
Power Member
Posts: 5406
Joined: 2006-01-26, 22:15 UTC
Location: Europe, Aachen

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *HolgerK »

History.txt wrote:16.12.19 Fixed: GDI leak (region) in Configuration - Options - Color when drawing color comboboxes (32/64)
Not completly fixed here (TC9.50b10x64 W10 1909)
It's somehow better but there are still GDI leaks.

- observation using GDIView (x64 version 1.2.6.0).
- test cycle was 10 times open and close the color configuration dialog.

-> In beta9: +86 region objects
-> In beta10: +50 font objects

so yes, the region problem is fixed but there is an new problem with fonts.

<OT>
I know, the GDI can be a pain.

Decades ago (last century) i had a similar problem and solved this by consequently using some helper classes to strictly follow the pattern:
{
auto variable Class constructor:
- CreateObject (e.g . CreatePen)
- SelectObject into device context + remember the previous selected object (return value of the SelectObject function)

Range body:
- Use the selected GDI objects (drawing lines, text and so on)

auto variable Class destructor:
- Deselect the object from DeviceContext by selecting the previous selected object
- Delete the object
}

Done via a C++ class constructor and automatic destructor-call when the local created auto (stack) variable was out of focus / leaving the range of the surrounding {block}.
This won't work with .net c# classes because of non deterministic lifetime (GarbageCollector!) but similar patterns like IDisposeable and using can be used instead.
I don't know much about modern Pascal (Delphi or Lazarus) but i guess this language offers similar possibilities.

Meanwhile the GDI seems to have some more sophisticated algorithms to fight against the "do not delete a gdi object while it is selected into a device context" memory leaks (see: https://devblogs.microsoft.com/oldnewthing/?p=5043 ) but i would not rely on this.
</OT>

Regards
Holger
Make our planet great again
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *ghisler(Author) »

In beta10: +50 font objects
I do not get this here, see my screenshot.
Do you get this with your 9.22ax64_gdi.ini?

Please note that Delphi/Lazarus caches a lot of GDI objects, so you should only start counting after opening and closing a dialog at least once.
Author of Total Commander
https://www.ghisler.com
User avatar
HolgerK
Power Member
Power Member
Posts: 5406
Joined: 2006-01-26, 22:15 UTC
Location: Europe, Aachen

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *HolgerK »

Do you get this with your 9.22ax64_gdi.ini?
Slightly modified ini-File with enabled dark mode:
test9.50x64_gdi.ini

Code: Select all

[Configuration]
UseNewDefFont=1
SetEncoding=ao.do.not.remove
firstmnu=2234
FirstTimeIconLib=0
FirstTime=0
test=15
SeparateTree=0
PanelsVertical=0
DarkMode=2
[FileSystemPlugins64]
$checksum$=3014672
[ButtonbarCache]
IconDll_default.bar=
IconDll_vertical.bar=
[1920x1080 (8x16)]
CmdSelX=636
CmdSelY=329
CmdSelDX=653
CmdSelDY=371
CmdSelMax=0
[Shortcuts]
C=cm_ColorConfig
[left]
path=c:\emptyDir\
ViewMode=10001
activepanelcolor=-1
activepanelcolor2=-1
ShowAllDetails=1
SpecialView=0
show=1
sortorder=0
negative Sortorder=0
activepanelcolordark=-1
activepanelcolordark2=-1
[right]
path=c:\emptyDir\
ViewMode=10001
activepanelcolor=-1
activepanelcolor2=-1
ShowAllDetails=1
SpecialView=0
show=1
sortorder=0
negative Sortorder=0
activepanelcolordark=-1
activepanelcolordark2=-1
[RightHistory]
0=c:\emptyDir\	#0
1=c:\	#5,emptyDir
[LeftHistory]
0=c:\emptyDir\	#0
1=c:\	#5,emptyDir
Please note that Delphi/Lazarus caches a lot of GDI objects, so you should only start counting after opening and closing a dialog at least once.
Yes, I've noticed this and triggered a counter reset after one initial open dialog, close dialog cycle:
-> http://holgerk.drivehq.com//TC9.50Beta/TC9.50Beta(AfterInitialColorConf).png
- reset counter
- 10 times <C>, <Escape>
-> http://holgerk.drivehq.com/TC9.50Beta/TC9.50Beta(After10TimesColorConf).png

http://holgerk.drivehq.com/TC9.50Beta/SystemInformation.png

Please note that the font GDI leak is also present in the today's test results for 9.50b9 and the region leak is smaller than yesterday.
May be that yesterday I've triggered one or two cycles more than today while testing with beta9 and overseen the font values in 9.50b9 result (long day yesterday).
Or the problem is not 100% reproducible.

Regards
Holger
Make our planet great again
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *ghisler(Author) »

I have investigated this a bit further:
1. It seems to be a bug in Windows 10 1909 (and maybe other Windows 10 versions), there is no leak in Windows 10 1803.
2. It seems to happen when using Lazarus comboboxes with edit control, where the editor part is subclassed by Lazarus. Apparently a font is set for that edit control, which is then lost when the dialog is closed. There is no loss when I remove the subclassing of the edit control, but then some functions like closing the dialog with ESC while the control has the focus don't work.
3. I haven't found a workaround for this Windows bug yet. Maybe I can set a different font before the control is destroyed, or remove the subclass. It will probably take a few hours to resolve.
Author of Total Commander
https://www.ghisler.com
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *ghisler(Author) »

3. Update: To fix this Windows 10 bug, I need to retrieve the handle of the inner edit control with GetComboBoxInfo, and destroy it with DestroyWindow before destroying the combobox. This seems to work fine so far.
Author of Total Commander
https://www.ghisler.com
User avatar
HolgerK
Power Member
Power Member
Posts: 5406
Joined: 2006-01-26, 22:15 UTC
Location: Europe, Aachen

Re: TC 9.5b9 - Color-Config GDI Memory leak

Post by *HolgerK »

history.txt wrote:23.12.19 Fixed: GDI leak (font) when opening and closing any dialog with comboboxes to which WM_SETFONT was sent (bug in Windows 10 1909 but not 1803) -> explicitly delete the edit box part with DestroyWindow (64)
I can confirm the fix using TC 9.50b11 x64 (W10 1909) .

Unfortunately i stumbled over another font leak while testing.

Steps to reproduce (same ini as in above test):
1. open "Color Config"
2. navigate to "Others:" combobox
3. press <down> until every additional color is previewed
4. close the dialog
-> 5 additional font handles.

BTW: the internal eng. language string 5266="Others:" is missing a shortcut (in german it has one: 5266="&Andere:").

Regards
Holger
Make our planet great again
Post Reply