0

I am trying to call the function GetParent() of CWnd class. Every time there is an exception thrown. I'm calling the function from a working thread.

This is the line that causes the exception: CWnd* parent = this->GetParent();

I've also tried "GetParent()->PostMessage(........);", and still the exception is thrown.

I'm using this method in a CDialog.

I have noticed that the CWnd member m_hWnd is sometimes 0x00000000 or 0x00000001. In a different computer I don't get this exception. Is it a problem in the project settings or in my code?

g-los
  • 33
  • 1
  • 7
  • Post the relevant code (5-6 lines of code around the line with the error) – sashoalm Feb 19 '13 at 12:30
  • As CDialog derives from CWnd, it must have a member named m_hWnd, check in a debugger if the value is != 0. – bash.d Feb 19 '13 at 12:35
  • You say "from a working thread." Does this mean that you have created this CWnd class on a thread started by AfxBeginThread, and if so have you [followed msdn.microsoft.com](http://msdn.microsoft.com/en-us/library/b807sta6(v=vs.100).aspx) for overriding the InitInstance function? Since this works on a different computer, and as @neagoegab points out your m_hWnd is not a valid window, you may be dealing with a race condition where the GetParent() is being called before the CWnd is created and initialized. – Mark Taylor Feb 19 '13 at 16:07

1 Answers1

2

You receive an assertion and not an exception.

Here is the implementation from VS 2010:

ASSERT(::IsWindow(m_hWnd)); return CWnd::FromHandle(::GetParent(m_hWnd));

Your m_hWnd is not a window...

joy
  • 1,569
  • 8
  • 11