[Solved] My app slows down other apps?

Everything else that doesn't fall into one of the other PB categories.
Axolotl
Addict
Addict
Posts: 837
Joined: Wed Dec 31, 2008 3:36 pm

Re: My app slows down other apps?

Post 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.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: My app slows down other apps?

Post 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?
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: My app slows down other apps?

Post by BarryG »

Don't worry about this; I've decided to discontinue my app. Time to move on from it. Thanks anyway!
User avatar
skywalk
Addict
Addict
Posts: 4218
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [Ignore] My app slows down other apps?

Post 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!
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Mohawk70
Enthusiast
Enthusiast
Posts: 404
Joined: Thu May 11, 2006 1:04 am
Location: Florida, USA

Re: [Ignore] My app slows down other apps?

Post by Mohawk70 »

Have you tried both OpenLibrary() , then make your function call(s) , then CloseLibrary() each time you make use of Kernel32.dll ?
HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: My app slows down other apps?

Post 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.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: My app slows down other apps?

Post by BarryG »

Last edited by BarryG on Wed Aug 14, 2024 1:26 am, edited 1 time in total.
AZJIO
Addict
Addict
Posts: 2190
Joined: Sun May 14, 2017 1:48 am

Re: My app slows down other apps?

Post by AZJIO »

I don't have a Handles tab
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: My app slows down other apps?

Post 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 :cry:
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: [Solved] My app slows down other apps?

Post by BarryG »

Solved! :) See first post in this topic.
User avatar
idle
Always Here
Always Here
Posts: 5895
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: [Solved] My app slows down other apps?

Post 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. :D
That's good news Barry. Can you elaborate on how it was causing the gdi leak and how you fixed it.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: [Solved] My app slows down other apps?

Post 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?
AZJIO
Addict
Addict
Posts: 2190
Joined: Sun May 14, 2017 1:48 am

Re: [Solved] My app slows down other apps?

Post 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.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: [Solved] My app slows down other apps?

Post 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.
Fred
Administrator
Administrator
Posts: 18220
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [Solved] My app slows down other apps?

Post by Fred »

Could you try to reproduce it in a small snippet ?
Post Reply