Page 3 of 4
Re: My app slows down other apps?
Posted: Tue Jul 23, 2024 1:43 pm
by Axolotl
Something I would always recommend. Read the Remark Section on every used api function very carefully. Especially when you use example codes.
Examples are usually small and clear, and error handling is often omitted. Typical example: No checking the return values!
I'm pretty sure you know that.
Acc. to your question. It depends. See this quote from MSDN:
After painting with a common DC, the ReleaseDC function must be called to release the DC. Class and private DCs do not have to be released. ReleaseDC must be called from the same thread that called GetDC. The number of DCs is limited only by available memory.
Re: My app slows down other apps?
Posted: Tue Jul 23, 2024 1:48 pm
by BarryG
Yeah, I always add my own error-checking if the examples don't include it.
But I noticed today that just switching PanelGadget tabs in my app will add an extra 1 or 2 GDI objects to the count in Task Manager! Just for switching tabs? No wonder the count is going up so much. I have no idea why this is because there's no code doing anything when switching. But this doesn't occur with the PanelGadget() example in the manual, so something in my app is going wrong. Damn.
Anyone know if there's an API or something to globally release any unused GDI objects (like a garbage collection), since I can't find the error in my code? Just as a stop-gap solution until I eventually find it?
Re: My app slows down other apps?
Posted: Wed Jul 24, 2024 4:38 am
by BarryG
Don't worry about this; I've decided to discontinue my app. Time to move on from it. Thanks anyway!
Re: [Ignore] My app slows down other apps?
Posted: Wed Jul 24, 2024 3:27 pm
by skywalk
Thanks for the post either way. This forced me to re-verify my code for instances of GDI leaks.
To be frank, I have very few cases of device context control.
My main use is for screen grabs of my app for documentation or printing.
Other than that, PB has enough gadgets to support my visual needs.
My summary of possible GDI leaks.
Code: Select all
; Avoid GDI memory leaks:
; https://www.deleaker.com/blog/2021/12/16/gdi-leaks-how-to-identify-and-fix-them/
; Display GDI objects count with Windows Performance Analyzer and Task Manager.
; Release objects using correct function depending on GDI object’s type and allocation.
; Device contexts:
; CreateDC -> use it -> Release with DeleteDC.
; GetWindowDC -> use it -> Release with ReleaseDC.
; GetDC -> use it -> Release with ReleaseDC.
; Objects like pens, brushes, fonts, bitmaps, regions, and palettes:
; If exists, copy object and select it back at end of use.
; use it -> Release with DeleteObject.
; Do not pass DC handles to DeleteObject, else leak.
; Painting is frequent and will leak fast!
Re: [Ignore] My app slows down other apps?
Posted: Wed Jul 24, 2024 5:17 pm
by Mohawk70
Have you tried both OpenLibrary() , then make your function call(s) , then CloseLibrary() each time you make use of Kernel32.dll ?
Re: My app slows down other apps?
Posted: Thu Jul 25, 2024 2:44 am
by BarryG
@skywalk: Thanks for the summary. I might have to go through my app and double-check all those situations, but currently not motivated. As mentioned, I also get the GDI count increasing in Task Manager just by switching tabs on the PanelGadget, so don't know how to resolve that. Maybe this is actually a PureBasic bug? I'll have to test with an older version.
@Mohawk70: I haven't done that, no. Maybe I should. I always read that's slower, though? Which is why I never did it. I'll try it anyway since that's easy to do. Don't see how that will resolve the GDI count when switching tabs on the PanelGadget, though.
Re: My app slows down other apps?
Posted: Thu Jul 25, 2024 7:57 am
by BarryG
Re: My app slows down other apps?
Posted: Thu Jul 25, 2024 9:25 am
by AZJIO
I don't have a Handles tab
Re: My app slows down other apps?
Posted: Thu Jul 25, 2024 12:52 pm
by Caronte3D
I have the same problem

My program is huge and I can't isolate the DC leak problem, even with this nice tool. I was isolate every DC API function that appears in my source, but nothing

Re: [Solved] My app slows down other apps?
Posted: Wed Aug 14, 2024 1:27 am
by BarryG
Solved!

See first post in this topic.
Re: [Solved] My app slows down other apps?
Posted: Wed Aug 14, 2024 1:52 am
by idle
BarryG wrote: Thu Jul 11, 2024 10:55 am
SOLVED! Turns out I had a
lot of regular loaded icons that were never being destroyed after use. Now my GDI count doesn't rise.
That's good news Barry. Can you elaborate on how it was causing the gdi leak and how you fixed it.
Re: [Solved] My app slows down other apps?
Posted: Wed Aug 14, 2024 2:31 am
by BarryG
My old routines were loading icons for displaying in a ListIconGadget that got cleared and rebuilt as needed, which is quite regularly (several times an hour) depending on what the user needed.
The start of my app has a bunch of icons embedded like this:
Code: Select all
Global Catch_Img_Document=ImageID(CatchImage(#PB_Any,?Img_Document))
Global Catch_Img_Folder=ImageID(CatchImage(#PB_Any,?Img_Folder))
When refreshing the ListIconGadget, I was doing it like this:
Code: Select all
For i=1 To itemcount
Select icon$
Case "document" : icon=Catch_Img_Document
Case "folder" : icon=Catch_Img_Folder
EndSelect
AddGadgetItem(#LIG,0,text$,icon)
Next
That was causing the GDI leak. It seems just copying the
handle of an icon to a
variable ("icon") was creating a new icon behind the scenes?
So all I did to fix it was copy the icon to the variable, add the copy to the ListIconGadget, and then destroy the copy:
Code: Select all
For i=1 To itemcount
Select icon$
Case "document" : icon=CopyIcon_(Catch_Img_Document)
Case "folder" : icon=CopyIcon_(Catch_Img_Folder)
EndSelect
AddGadgetItem(#LIG,0,text$,icon)
DestroyIcon_(icon) ; Prevents GDI leak.
Next
@Fred: Should adding icons to a ListIconGadget like my first example even cause a GDI leak? Maybe it's a PureBasic bug?
Re: [Solved] My app slows down other apps?
Posted: Wed Aug 14, 2024 6:38 am
by AZJIO
I don't see any leak in the code. If you created a new icon handle each time, then there would be a leak. If you constantly use the same handle, then you have one object in memory. You release them only when you close the program.
Re: [Solved] My app slows down other apps?
Posted: Wed Aug 14, 2024 6:54 am
by BarryG
I know what you mean AZJIO, but the GDI count in Task Manager kept going up until I changed the loop to copy the icon and destroy the copy after adding it. I can't come to any other conclusion based on that fact. Literally nothing else in my app changed while testing this.
Re: [Solved] My app slows down other apps?
Posted: Wed Aug 14, 2024 7:03 am
by Fred
Could you try to reproduce it in a small snippet ?