SetBackground UnRef causing exception

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
PB
wxWorld Domination!
wxWorld Domination!
Posts: 1276
Joined: Sun Jan 03, 2010 5:45 pm

Re: SetBackground UnRef causing exception

Postby PB » Thu Sep 14, 2017 11:09 am

GDI handles can also leak outside obvious painting code, e.g. allocating wxFont, wxBitmap, wxIcon etc. to pass it to a control....

Telemotive
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Sep 07, 2017 7:59 am

Re: SetBackground UnRef causing exception

Postby Telemotive » Mon Sep 18, 2017 10:11 am

doublemax wrote:I think it's safe to say that there is no memory leak in that paint event code. Also, the number of handles seems almost constant after the window has been shown once. Just to be sure: How is the paint event handler connected?


What do you mean with connected?
We just overwrote the wxPanel onPaint() method:

Code: Select all

class SettingsGridPanel : public wxPanel


doublemax wrote:I assume the settings window is opened above the main window? Does that have any custom paint code? Showing and hiding the settings window will also cause a paint event in the underlying window. Maybe the problem lies there. If yes, you should also see the problem if you just move a window from another application over your main window, so that lots of paint events are generated.


Yea, our application has one small window for main settings, a button to open an advanced settings dialog and a button to start the test.
The test progress is shown in another two windows, one dialog with a progress bar and a label telling the current test step.
The second window shows test logs and further test details.

I tried as you told me but no paint event entered the onPaint() code i previously posted here.
When I resize the windows a onPaint() event reaches my code.

Telemotive
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Sep 07, 2017 7:59 am

Re: SetBackground UnRef causing exception

Postby Telemotive » Mon Sep 18, 2017 10:12 am

PB wrote:GDI handles can also leak outside obvious painting code, e.g. allocating wxFont, wxBitmap, wxIcon etc. to pass it to a control....


Thanks, but I already checked our usages of wxFont, wxBitmap, wxIcon etc. and did not find any issues.

User avatar
eranon
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 610
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: SetBackground UnRef causing exception

Postby eranon » Mon Sep 18, 2017 12:06 pm

Did you try to do a dynamic code analysis through tools like BoundsChecker, Dr. Memory, Purify... https://en.wikipedia.org/wiki/Dynamic_program_analysis?
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]

Telemotive
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Sep 07, 2017 7:59 am

Re: SetBackground UnRef causing exception

Postby Telemotive » Mon Sep 18, 2017 4:10 pm

eranon wrote:Did you try to do a dynamic code analysis through tools like BoundsChecker, Dr. Memory, Purify... https://en.wikipedia.org/wiki/Dynamic_program_analysis?


Hey,

we already use Dr. Memory and I have also checked the reported leaks, but I am not able to identify the source.
Maybe you can help me:

This one appears on application start

Code: Select all

Error #1: GDI USAGE ERROR: DC 0xffffffffc6011cf1 that contains selected object being deleted
         system call NtGdiDeleteObjectApp
             <system call>
         GDI32.dll!GetRandomRgn
             ??:0
         wxDC::~wxDC       
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\dc.cpp(314):
         wxNotebook::OnPaint
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\notebook.cpp(957):
         wxEvtHandler::ProcessEventIfMatches
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\common\event.cpp(1239):
         wxEventHashTable::HandleEvent
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\common\event.cpp(907):
         wxRegion::~wxRegion
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\region.cpp(144):
         wxEvtHandler::ProcessEvent
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\common\event.cpp(1301):
         wxWindow::HandlePaint
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(4607):
         wxWindow::MSWWindowProc
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(2747):
         UxTheme.dll!?         
             ??:0
         UxTheme.dll!?         
             ??:0
         UxTheme.dll!?         
             ??:0
         wxWndProc         
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(2618):
         USER32.dll!TranslateMessageEx
             ??:0
         USER32.dll!SetWindowTextW
             ??:0
         USER32.dll!IsDialogMessageW
             ??:0
         ntdll.dll!KiUserCallbackDispatcher
             ??:0
         wxWindow::MSWProcessMessage
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(2407):
         wxFrame::MSWDoTranslateMessage
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\frame.cpp(825):
         Note: DC was allocated here:
                    system call NtGdiCreateCompatibleDC
             <system call>
                    GDI32.dll!CreateCompatibleDC
             ??:0
                    wxMemoryDC::CreateCompatible
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\dcmemory.cpp(74):
                    wxNotebook::OnPaint
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\notebook.cpp(927):
                    wxEvtHandler::ProcessEventIfMatches
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\common\event.cpp(1239):
                    wxEventHashTable::HandleEvent
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\common\event.cpp(907):
                    wxRegion::~wxRegion
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\region.cpp(144):
                    wxEvtHandler::ProcessEvent
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\common\event.cpp(1301):
                    wxWindow::HandlePaint
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(4607):
                    wxWindow::MSWWindowProc
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(2747):
                    UxTheme.dll!?         
             ??:0
                    UxTheme.dll!?         
             ??:0
                    UxTheme.dll!?         
             ??:0
                    wxWndProc         
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(2618):
                    USER32.dll!TranslateMessageEx
             ??:0
                    USER32.dll!SetWindowTextW
             ??:0
                    USER32.dll!IsDialogMessageW
             ??:0
                    ntdll.dll!KiUserCallbackDispatcher
             ??:0
                    wxWindow::MSWProcessMessage
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(2407):
                    wxFrame::MSWDoTranslateMessage
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\frame.cpp(825):
 


These two appear after opening the settings window

Code: Select all

Error #2: GDI USAGE ERROR: DC 0x000000006e01272a that contains selected object being deleted
         system call NtUserCallOneParam.RELEASEDC
             <system call>
         USER32.dll!ReleaseDC
             ??:0
         COMCTL32.dll!Ordinal341 
             ??:0
         COMCTL32.dll!Ordinal341 
             ??:0
         wxWindow::MSWDefWindowProc
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(2240):
         KERNEL32.dll!MultiByteToWideChar
             ??:0
         COMCTL32.dll!Ordinal341 
             ??:0
         COMCTL32.dll!Ordinal341 
             ??:0
         USER32.dll!TranslateMessageEx
             ??:0
         USER32.dll!CallWindowProcW
             ??:0
         USER32.dll!CallWindowProcA
             ??:0
         wxNotebookWndProc 
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\notebook.cpp(915):
         USER32.dll!TranslateMessageEx
             ??:0
         USER32.dll!CallWindowProcW
             ??:0
         USER32.dll!CallWindowProcA
             ??:0
         wxWindow::MSWDefWindowProc
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(2240):
         wxWindow::MSWWindowProc
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(3427):
         UxTheme.dll!DrawThemeParentBackground
             ??:0
         UxTheme.dll!DrawThemeParentBackground
             ??:0
         UxTheme.dll!DrawThemeParentBackground
             ??:0
         Note: DC was allocated here:
                    system call NtUserGetDC
             <system call>
                    COMCTL32.dll!Ordinal341 
             ??:0
                    COMCTL32.dll!Ordinal341 
             ??:0
                    wxWindow::MSWDefWindowProc
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(2240):
                    KERNEL32.dll!MultiByteToWideChar
             ??:0
                    COMCTL32.dll!Ordinal341 
             ??:0
                    COMCTL32.dll!Ordinal341 
             ??:0
                    USER32.dll!TranslateMessageEx
             ??:0
                    USER32.dll!CallWindowProcW
             ??:0
                    USER32.dll!CallWindowProcA
             ??:0
                    wxNotebookWndProc 
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\notebook.cpp(915):
                    USER32.dll!TranslateMessageEx
             ??:0
                    USER32.dll!CallWindowProcW
             ??:0
                    USER32.dll!CallWindowProcA
             ??:0
                    wxWindow::MSWDefWindowProc
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(2240):
                    wxWindow::MSWWindowProc
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\window.cpp(3427):
                    UxTheme.dll!DrawThemeParentBackground
             ??:0
                    UxTheme.dll!DrawThemeParentBackground
             ??:0
                    UxTheme.dll!DrawThemeParentBackground
             ??:0
                    UxTheme.dll!?         
             ??:0


Code: Select all

Error #4: GDI USAGE ERROR: DC 0xffffffffdd011a7c that contains selected object being deleted
         system call NtGdiDeleteObjectApp
             <system call>
         GDI32.dll!GetRandomRgn
             ??:0
         wxToolTip::Add     
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\tooltip.cpp(375):
         wxToolTip::SetWindow
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\tooltip.cpp(402):
         wxWindowBase::SetToolTip
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\common\wincmn.cpp(1714):
         MeasuringSettingPage::MeasuringSettingPage
             c:\sspandl\develop_1_5_1\funktionstester\source\fktlibgeneraldataloggertester\datasourceconfigurationpages\measuringsettingpage.cc(21):
         SystemTestNBPage::SystemTestNBPage
             c:\sspandl\develop_1_5_1\funktionstester\source\funktionstesteruserinterface\systemtestnbpage.cc(66):
         wxNotebookWndProc 
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\notebook.cpp(915):
         USER32.dll!TranslateMessageEx
             ??:0
         USER32.dll!SetScrollInfo
             ??:0
         USER32.dll!GetWindowLongW
             ??:0
         USER32.dll!IsWindow 
             ??:0
         USER32.dll!IsServerSideWindow
             ??:0
         USER32.dll!TranslateMessageEx
             ??:0
         USER32.dll!TranslateMessageEx
             ??:0
         USER32.dll!TranslateMessageEx
             ??:0
         USER32.dll!SetWindowTextW
             ??:0
         USER32.dll!GetSystemMenu
             ??:0
         wxWindowBase::InvalidateBestSize
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\common\wincmn.cpp(514):
         wxStringData::Unlock
             c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\include\wx\string.h(243):
         Note: DC was allocated here:
                    system call NtGdiCreateCompatibleDC
             <system call>
                    GDI32.dll!CreateCompatibleDC
             ??:0
                    wxToolTip::Add     
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\tooltip.cpp(343):
                    wxToolTip::SetWindow
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\tooltip.cpp(402):
                    wxWindowBase::SetToolTip
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\common\wincmn.cpp(1714):
                    MeasuringSettingPage::MeasuringSettingPage
                        c:\sspandl\develop_1_5_1\funktionstester\source\fktlibgeneraldataloggertester\datasourceconfigurationpages\measuringsettingpage.cc(21):
                    SystemTestNBPage::SystemTestNBPage
                        c:\sspandl\develop_1_5_1\funktionstester\source\funktionstesteruserinterface\systemtestnbpage.cc(66):
                    wxNotebookWndProc 
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\notebook.cpp(915):
                    USER32.dll!TranslateMessageEx
             ??:0
                    USER32.dll!SetScrollInfo
             ??:0
                    USER32.dll!GetWindowLongW
             ??:0
                    USER32.dll!IsWindow 
             ??:0
                    USER32.dll!IsServerSideWindow
             ??:0
                    USER32.dll!TranslateMessageEx
             ??:0
                    USER32.dll!TranslateMessageEx
             ??:0
                    USER32.dll!TranslateMessageEx
             ??:0
                    USER32.dll!SetWindowTextW
             ??:0
                    USER32.dll!GetSystemMenu
             ??:0
                    wxWindowBase::InvalidateBestSize
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\common\wincmn.cpp(514):
                    wxStringData::Unlock
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\include\wx\string.h(243):

User avatar
eranon
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 610
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: SetBackground UnRef causing exception

Postby eranon » Mon Sep 18, 2017 4:45 pm

I'm not used to use Dr. Memory specially, but according to their doc, it's not necessary relevant talking about recent Windows version: http://drmemory.org/docs/page_gdi.html (and in their topics -- at left side of the help pages --, it seems they separate memory leaks and GDI usage errors). So, maybe trying different analysis tools will help to figure out the point(s) of failure by crosscheck...
Last edited by eranon on Mon Sep 18, 2017 9:35 pm, edited 2 times in total.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]

User avatar
doublemax
Moderator
Moderator
Posts: 11300
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: SetBackground UnRef causing exception

Postby doublemax » Mon Sep 18, 2017 4:49 pm

We just overwrote the wxPanel onPaint() method:
wxWidgets event handlers are not implemented by overriding virtual methods. This method can have any name, there must be either a static event table or a Connect() call somewhere.

Code: Select all

wxNotebook::OnPaint
                        c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\notebook.cpp(927):
It's suspicious that the problem lies in the wxNotebook paint event handler. Do you have any class that derives from wxNotebook?

Seeing this, it's even possible that this is a bug in wxWidgets which has been fixed many years ago. Have you ever tried to compile your project under wx 3.1.x? Or did you just assume that it's a lot of work?

For a test, disable the paint event handler in SettingsGridPanel and check if the leak still exists. If yes, we've been searching in the wrong spot all the time.
Use the source, Luke!

User avatar
eranon
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 610
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: SetBackground UnRef causing exception

Postby eranon » Tue Sep 19, 2017 11:42 am

If it can help determining if the info is relevant or not, running Dr. Memory (1.11) against one of my own apps (launching the program, then exiting immediately), I got "13 total GDI usage errors" whose 12 about wxToolTip and one about wxNotebook:

Code: Select all

Error #35: GDI USAGE ERROR: DC 0x5f011a41 that contains selected object being deleted
# 0 system call NtGdiDeleteObjectApp     
# 1 GDI32.dll!DeleteDC                                 +0xb6     (0x7696596a <GDI32.dll+0x1596a>)
# 2 GDI32.dll!DeleteDC                                 +0x11     (0x769658c5 <GDI32.dll+0x158c5>)
# 3 wxMSWDCImpl::~wxMSWDCImpl                           [../../src/msw/dc.cpp:512]
# 4 wxMemoryDCImpl::~wxMemoryDCImpl                     [../../include/wx/msw/dcmemory.h:17]
# 5 wxMemoryDCImpl::~wxMemoryDCImpl                     [../../include/wx/msw/dcmemory.h:17]
# 6 wxDC::~wxDC                                         [C:/devlibs/wxWidgets-3.1.0/include/wx/dc.h:742]
# 7 wxMemoryDC::~wxMemoryDC                             [C:/devlibs/wxWidgets-3.1.0/include/wx/dcmemory.h:21]
# 8 wxNotebook::OnPaint                                 [../../src/msw/notebook.cpp:882]
# 9 wxAppConsoleBase::HandleEvent                       [../../src/common/appbase.cpp:657]
#10 wxAppConsoleBase::CallEventHandler                  [../../src/common/appbase.cpp:669]
#11 wxEvtHandler::ProcessEventIfMatchesId               [../../src/common/event.cpp:1381]
#12 wxEventHashTable::HandleEvent                       [../../src/common/event.cpp:989]
#13 wxEvtHandler::TryHereOnly                           [../../src/common/event.cpp:1578]
#14 wxEvtHandler::TryBeforeAndHere                      [../../include/wx/event.h:3656]
#15 wxEvtHandler::ProcessEventLocally                   [../../src/common/event.cpp:1511]
#16 wxEvtHandler::ProcessEvent                          [../../src/common/event.cpp:1484]
#17 wxEvtHandler::SafelyProcessEvent                    [../../src/common/event.cpp:1602]
#18 wxWindowBase::HandleWindowEvent                     [../../src/common/wincmn.cpp:1534]
#19 wxWindow::HandlePaint                               [../../src/msw/window.cpp:4719]
Note: @0:00:59.778 in thread 8248
Note: DC was allocated here:
Note: # 0 system call NtGdiCreateCompatibleDC 
Note: # 1 GDI32.dll!CreateCompatibleDC                       +0xf      (0x76965504 <GDI32.dll+0x15504>)
Note: # 2 wxMemoryDCImpl::CreateCompatible                    [../../src/msw/dcmemory.cpp:90]
Note: # 3 wxMemoryDCImpl::wxMemoryDCImpl                      [../../src/msw/dcmemory.cpp:45]
Note: # 4 wxNativeDCFactory::CreateMemoryDC                   [../../src/common/dcbase.cpp:149]
Note: # 5 wxMemoryDC::wxMemoryDC                              [../../src/common/dcbase.cpp:214]
Note: # 6 wxNotebook::OnPaint                                 [../../src/msw/notebook.cpp:802]
Note: # 7 wxAppConsoleBase::HandleEvent                       [../../src/common/appbase.cpp:657]
Note: # 8 wxAppConsoleBase::CallEventHandler                  [../../src/common/appbase.cpp:669]
Note: # 9 wxEvtHandler::ProcessEventIfMatchesId               [../../src/common/event.cpp:1381]
Note: #10 wxEventHashTable::HandleEvent                       [../../src/common/event.cpp:989]
Note: #11 wxEvtHandler::TryHereOnly                           [../../src/common/event.cpp:1578]
Note: #12 wxEvtHandler::TryBeforeAndHere                      [../../include/wx/event.h:3656]
Note: #13 wxEvtHandler::ProcessEventLocally                   [../../src/common/event.cpp:1511]
Note: #14 wxEvtHandler::ProcessEvent                          [../../src/common/event.cpp:1484]
Note: #15 wxEvtHandler::SafelyProcessEvent                    [../../src/common/event.cpp:1602]
Note: #16 wxWindowBase::HandleWindowEvent                     [../../src/common/wincmn.cpp:1534]
Note: #17 wxWindow::HandlePaint                               [../../src/msw/window.cpp:4719]
Note: #18 wxWindow::MSWHandleMessage                          [../../src/msw/window.cpp:2810]
Note: #19 wxWindow::MSWWindowProc                             [../../src/msw/window.cpp:3524]

Done under Windows 7 64-bit with a 32-bit DEBUG build of the app (wx31-based).
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]

User avatar
doublemax
Moderator
Moderator
Posts: 11300
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: SetBackground UnRef causing exception

Postby doublemax » Tue Sep 19, 2017 2:07 pm

eranon wrote:If it can help determining if the info is relevant or not, running Dr. Memory (1.11) against one of my own apps (launching the program, then exiting immediately), I got "13 total GDI usage errors" whose 12 about wxToolTip and one about wxNotebook:
Thanks. So it's possible that the errors in the wxNoteBook paint event handler are not relevant after all. This is getting more confusing all the time.

To OP: If you can't provide a sample that demonstrates the issue, i can only recommend to start commenting out parts of your code that deal with GDI objects until you've narrowed down the code location.

If you can't post relevant code publicly, you can also PM me. Maybe we can figure something out.
Use the source, Luke!


Return to “C++ Development”

Who is online

Users browsing this forum: No registered users and 13 guests