Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Command Bars
  New Posts New Posts RSS Feed - Changing text in Statusbar pane from timer event
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Changing text in Statusbar pane from timer event

 Post Reply Post Reply
Author
Message
mitcheljh View Drop Down
Groupie
Groupie


Joined: 20 July 2008
Location: United States
Status: Offline
Points: 35
Post Options Post Options   Thanks (0) Thanks(0)   Quote mitcheljh Quote  Post ReplyReply Direct Link To This Post Topic: Changing text in Statusbar pane from timer event
    Posted: 20 March 2009 at 6:48am
Hi,

I have a timer (WM_TIMER) which triggers the changing of text in a statusbar pane at regular intervals (eg 1 second). I'm noticing that the application crashes intermittently, not when setting the text, but when retrieving the text while the statusbar is being redrawn. Sometimes the crash occurs minutes after starting the app, sometimes much longer.

So far, I'm only noticing this issue in the debug build.

After the crash, the debugger leaves me at the following line in xtpstatusbar.cpp, and the variable m_strText always contains the correct current text that's displayed in the pane:

CString CXTPStatusBarPane::GetText() const
{
return m_strText;
}


Below is the stack trace which leads up to the crash:


     mfc71d.dll!ATL::CSimpleStringT<char,1>::CloneData(ATL::CStringData * pData=0x022a3c60) Line 804 + 0xa     C++
      mfc71d.dll!ATL::CSimpleStringT<char,1>::CSimpleStringT<char,1>(const ATL::CSimpleStringT<char,0> & strSrc={...}) Line 220 + 0x9     C++
      mfc71d.dll!ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >(const ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > & strSrc={...}) Line 815     C++
>     MyApp.exe!CXTPStatusBarPane::GetText() Line 184 + 0x12     C++
      MyApp.exe!CXTPPaintManager::DrawStatusBarPaneEntry(CDC * pDC=0x0012f924, CRect rcItem={...}, CXTPStatusBarPane * pPane=0x0388cd98) Line 2102 + 0xc     C++
      MyApp.exe!CXTPStatusBarPane::OnDraw(CDC * pDC=0x0012f924, CRect rcItem={...}) Line 266 + 0x34     C++
      MyApp.exe!CXTPStatusBar::DrawPaneEntry(CDC * pDC=0x0012f924, int nIndex=1, CRect rcItem={...}) Line 1513 + 0x2d     C++
      MyApp.exe!CXTPStatusBar::OnDraw(CDC * pDC=0x0012f924) Line 1579 + 0x3d     C++
      MyApp.exe!CXTPStatusBar::OnPaint() Line 1523     C++
      mfc71d.dll!CWnd::OnWndMsg(unsigned int message=15, unsigned int wParam=0, long lParam=0, long * pResult=0x0012fbf0) Line 2023     C++
      MyApp.exe!CXTPStatusBar::OnWndMsg(unsigned int message=15, unsigned int wParam=0, long lParam=0, long * pResult=0x0012fbf0) Line 1849     C++
      mfc71d.dll!CWnd::WindowProc(unsigned int message=15, unsigned int wParam=0, long lParam=0) Line 1745 + 0x1e     C++
      mfc71d.dll!CControlBar::WindowProc(unsigned int nMsg=15, unsigned int wParam=0, long lParam=0) Line 503 + 0x14     C++
      mfc71d.dll!AfxCallWndProc(CWnd * pWnd=0x02237240, HWND__ * hWnd=0x003812f8, unsigned int nMsg=15, unsigned int wParam=0, long lParam=0) Line 241 + 0x1a     C++
      mfc71d.dll!AfxWndProc(HWND__ * hWnd=0x003812f8, unsigned int nMsg=15, unsigned int wParam=0, long lParam=0) Line 389     C++
      mfc71d.dll!AfxWndProcBase(HWND__ * hWnd=0x003812f8




I'm wondering if this could be a threading issue, and if there's any workaround to the problem.

Thanks!


Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post Posted: 20 March 2009 at 10:00am
Are you running Vista? I had a similar crash (with a popup-menu) which only occurred in Vista.
PokerMemento - http://www.pokermemento.com/
Back to Top
mitcheljh View Drop Down
Groupie
Groupie


Joined: 20 July 2008
Location: United States
Status: Offline
Points: 35
Post Options Post Options   Thanks (0) Thanks(0)   Quote mitcheljh Quote  Post ReplyReply Direct Link To This Post Posted: 20 March 2009 at 10:33am
No, I'm running xp.
So far, I'm only seeing the issue in the debug build.
Also, it mainly happens when I'm working in another application. When the application containing the status bar that get's updated has focus, the crash doesn't seem to occur. It could be that I just haven't witnessed it occurring when it has the focus, though.
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 23 March 2009 at 1:59am
Hi,
How you set text ?
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
mitcheljh View Drop Down
Groupie
Groupie


Joined: 20 July 2008
Location: United States
Status: Offline
Points: 35
Post Options Post Options   Thanks (0) Thanks(0)   Quote mitcheljh Quote  Post ReplyReply Direct Link To This Post Posted: 23 March 2009 at 6:01am
I use...

CXTPStatusBarPane* pPane = pStatusBar->FindPane(myPane);
assert(pPane);
pPane->SetText(myText.c_str());


.. where myText is a std::string.

Back to Top
Smucker View Drop Down
Senior Member
Senior Member
Avatar

Joined: 02 February 2008
Status: Offline
Points: 156
Post Options Post Options   Thanks (0) Thanks(0)   Quote Smucker Quote  Post ReplyReply Direct Link To This Post Posted: 23 March 2009 at 7:18am
Are you setting pane text from a separate thread? If so, the setting of the pane's CString may be occurring simultaneously with its use by another thread. When the crash occurs, check the other threads to see if CXTPStatusBarPane::SetText is on the stack.

I usually concentrate all the UI in the main thread, and post messages from other threads for updates, and use synchronization for any objects shared between threads.

Product: Xtreme Toolkit Pro version 13.2 (Unicode, static build)

Platform: Windows 200x/XP/Vista/Win7 (32/64 bit)

Language: Visual C++ 9.0 (Studio 2008)

Back to Top
mitcheljh View Drop Down
Groupie
Groupie


Joined: 20 July 2008
Location: United States
Status: Offline
Points: 35
Post Options Post Options   Thanks (0) Thanks(0)   Quote mitcheljh Quote  Post ReplyReply Direct Link To This Post Posted: 23 March 2009 at 8:18am
Thanks for the suggestion and info, Phillip. The timer I'm using can be used either synchronously or asynchronously, and I've been using it asynchronously. I just changed it to work synchronously (with the main UI thread) and the crash is still occuring.

I think you may be correct about the issue being related to the threads though. If the pane is redrawn in a separate thread (is this true, Oleg?), it may be trying to redraw the pane at the same time I'm updating the text in that pane.   What is puzzling is that the issue only seems to be occuring in debug mode. I haven't had a crash (yet) in the release build.
Back to Top
mitcheljh View Drop Down
Groupie
Groupie


Joined: 20 July 2008
Location: United States
Status: Offline
Points: 35
Post Options Post Options   Thanks (0) Thanks(0)   Quote mitcheljh Quote  Post ReplyReply Direct Link To This Post Posted: 23 March 2009 at 9:17am
UPDATE:
It seems I missed another part of my code where I was setting the timer asynchronously. I changed this so that it is now using the timer synchronously, and so far, I haven't had any crashes.   So, it looks like this may have been the problem after all.
Thanks Phillip, and also Oleg and znakeeye for looking into this.

I'll update this message thread after a couple of days of testing, to inform everyone if setting the timer to work synchronously has indeed fixed the issue.
Back to Top
Smucker View Drop Down
Senior Member
Senior Member
Avatar

Joined: 02 February 2008
Status: Offline
Points: 156
Post Options Post Options   Thanks (0) Thanks(0)   Quote Smucker Quote  Post ReplyReply Direct Link To This Post Posted: 23 March 2009 at 9:33am
If WM_TIMER (and the call to SetText) are in a different thread than the one that paints the status pane (and calls GetText), I think you have a race condition in CString handling. Debug code takes more cycles so is sometimes more likely to cause the issue. I guess it could also include non-thread-safe code not in the release compile, but that would be weird. (SetTimer() is never asynchronous; supplying a routine simply automates calling the TimerProc from the default window proc, rather than having to handle WM_TIMER.)

Your stack trace above is from the drawing thread; check your other threads' stacks for calls to CXTPStatusBarPane methods. Also, double-click on the GetText() line and examine this->m_strText.

Also, what exception is being reported?


Another idea: change your timer to 10 to 50 ms and see if you can get it to crash quicker, and maybe in release mode. I assume you have at least a dual-core system; single-core systems hide (or make much rarer) a lot of multithreading issues.


Product: Xtreme Toolkit Pro version 13.2 (Unicode, static build)

Platform: Windows 200x/XP/Vista/Win7 (32/64 bit)

Language: Visual C++ 9.0 (Studio 2008)

Back to Top
mitcheljh View Drop Down
Groupie
Groupie


Joined: 20 July 2008
Location: United States
Status: Offline
Points: 35
Post Options Post Options   Thanks (0) Thanks(0)   Quote mitcheljh Quote  Post ReplyReply Direct Link To This Post Posted: 24 March 2009 at 6:57am
Thanks for the further info, Phillip.
It looks like this was indeed the issue (using an asynchronous timer). Trying your debugging suggestions, pointed the issue as being a threading issue.

The timer I'm using is a 3rd party high accuracy timer, and it can be ran either synchronously or asynchronously.
After changing all instances of it to run synchronously, all the crashes have stopped. I tried decreasing the interval time as you suggested and running synchronously, and still no crashes.

Switching the 3rd party to synchronous mode won't affect the accuracy of the timer, but may slightly affect the accuracy when the UI thread receives the regular messages, which is OK for my purposes.

Thanks again for the info and suggestions.





Back to Top
Smucker View Drop Down
Senior Member
Senior Member
Avatar

Joined: 02 February 2008
Status: Offline
Points: 156
Post Options Post Options   Thanks (0) Thanks(0)   Quote Smucker Quote  Post ReplyReply Direct Link To This Post Posted: 24 March 2009 at 12:13pm
Sorry for going on about threads, I thought it was the simple SetTimer since you were only using for updating.

APCs can cause reentrancy problems that are very similar to thread issues. Glad you got it sorted out!

Product: Xtreme Toolkit Pro version 13.2 (Unicode, static build)

Platform: Windows 200x/XP/Vista/Win7 (32/64 bit)

Language: Visual C++ 9.0 (Studio 2008)

Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.203 seconds.