I have a SDI application. I created a method OnClose to handle ON_WM_CLOSE of CMainFrm. This onclose() function calls a method in cmyview.cpp. Here, I created a thread that calls global function and from this function it calls another function in cmyview.cpp. At certain condition my application should close at here, I used postmessgae(WM_CLOSE (or) WM_DESTROY). I am having an error as object reference not set on postmessage(WM_CLOSE) it is going to afxwin2.inl page where exception occurs.

Below is code snippetHere, either j or k will only be true depneds on user input)

MainFrm.cpp:
void CMainFrame::OnClose()
{
CMyView* pview = (CMyView*)((CFrameWnd*)AfxGetMainWnd())->GetActiveView();
pview->method1();
}

CMyView.cpp:
void CMyView::method2()
{
int i =1;
bool j, k
while(i==1)
{
if (j)
{
i=2;
PostMessage(WM_CLOSE);
}
else if (j)
{
i=2;
}
}
}

UINT thredproc1(LPVOID lpvoid)
{
CMyView* pMyView = (CMyView*)lpvoid;
pMyView->method2();
return 0;
}

void CMyView::method1()
{
CWinThread* pthread = AfxBeginThread(thredproc1,this,0);
}