Re: SetBackground UnRef causing exception
Posted: 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....
Official forum for the wxWidgets Cross-Platform GUI Toolkit
https://forums.wxwidgets.org/
What do you mean with connected?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?
Code: Select all
class SettingsGridPanel : public wxPanel
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.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.
Thanks, but I already checked our usages of wxFont, wxBitmap, wxIcon etc. and did not find any issues.PB wrote:GDI handles can also leak outside obvious painting code, e.g. allocating wxFont, wxBitmap, wxIcon etc. to pass it to a control....
Hey,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?
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):
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):
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.We just overwrote the wxPanel onPaint() method:
Code: Select all
wxNotebook::OnPaint
c:\sspandl\develop_1_5_1\funktionstester\externallibs\wx2017_x64\src\msw\notebook.cpp(927):
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]
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.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:
yes, but there are too many to show them all hereDo you have any class that derives from wxNotebook?
We tried, but got many linking errors. I think I can remember because of an wxWidgets API change.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?
We did that and testing is still going on. We cant yet say it solved the problem, but the error has not occured yet.For a test, disable the paint event handler in SettingsGridPanel and check if the leak still exists.
We do not know under what circumstances the error happens, so I can not provide a sample.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.
You said you got an increase of GDI handles when you opened end closed the settings window. Shouldn't the change be visible immediately ?The GDI- and User-Object monitoring is implemented, but since that we did not have a crash, so I still can not say that it is a GDI- User-Object leak.
Yea, that is true, but that was just a test to check if there is a leak somewhere in general.You said you got an increase of GDI handles when you opened end closed the settings window. Shouldn't the change be visible immediately ?
Which leads me to think that, potentially, since your onPaint code wasnt properly connected to the event loop, your "this" pointer may lie outside of the GUI context, so you wind up with intermittent exceptions when trying to Ref and UnRef the GUI objects.doublemax wrote: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.What do you mean with connected?
We just overwrote the wxPanel onPaint() method:
...
no paint event entered the onPaint() code i previously posted here.