0

i'm working on a mfc application for embedded compact. At the moment i try to add a dialog which offers to close the programm. My problem is that i get an access violation at the destructor call from my CMainFrame. To make it more clear first a piece of code. This is the startpoint of my application:

SWinApp.h

class SWinApp : public CWinApp
{
public:
    SWinApp();
    ~SWinApp(){};
public:
    virtual BOOL InitInstance();

protected:
    DECLARE_MESSAGE_MAP()
};

SWinApp.cpp

SWinApp::SWinApp():CWinApp()
{
}

BOOL SWinApp::InitInstance()
{
    CRuntimeClass* pRuntimeClass = RUNTIME_CLASS( SMainFrame );
    CObject* pObject = pRuntimeClass->CreateObject();
    ASSERT( pObject->IsKindOf( RUNTIME_CLASS( SMainFrame ) ) );
    m_pMainWnd = (SMainFrame*)pObject;
    m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
    return FALSE; // this is the next executed line after the access violation
}

BEGIN_MESSAGE_MAP(SWinApp, CWinApp)
END_MESSAGE_MAP()

SMainFrame.h

class SMainFrame : public CFrameWnd
{
    DECLARE_DYNCREATE(SMainFrame)
protected:
    SMainFrame();           
    ~SMainFrame();
    DECLARE_MESSAGE_MAP()
public:
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

    afx_msg LRESULT OnDialogReady(WPARAM wParam, LPARAM lParam);

    afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);

    CurrentData* GetCurrentData(){return this->currentData;};
    Configuration* GetConfiguration(){return this->configuration;};
private:
    BOOL m_shown;   
    CurrentData* currentData;
    Configuration* configuration;
    std::map<UINT, CDialog*> views;
    UINT currentID;
};

SMainFrame.cpp

IMPLEMENT_DYNCREATE(SMainFrame, CFrameWnd)

    SMainFrame::SMainFrame()
{
    CString appName;
    appName.LoadStringW(IDS_APP_NAME);
    Create(NULL, appName);
    m_shown = FALSE;
    this->configuration = new Configuration();
    this->currentData = new CurrentData();
    this->currentID = IDD_MAIN_MENU_DIALOG;
    views.insert(std::make_pair(IDD_MAIN_MENU_DIALOG, new MainMenuDialog(this->configuration, this->currentData)));
    // There are more dialogs but for tests one is enough

}

SMainFrame::~SMainFrame()
{
    for(auto iterator:views)
    {
        delete iterator.second;
    }
    delete this->configuration;
    delete this->currentData; //Is executed properly
} // After this line an access violation occurres...


BEGIN_MESSAGE_MAP(SMainFrame, CFrameWnd)
    ON_MESSAGE(WM_USER_DIALOG_READY, &SMainFrame::OnDialogReady)
    ON_WM_CREATE()
    ON_WM_ACTIVATE()
END_MESSAGE_MAP()

// SMainFrame message handlers

int SMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    ModifyStyle(WS_CAPTION, 0, SWP_DRAWFRAME | SWP_NOZORDER );

    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    return 0;
}
afx_msg LRESULT SMainFrame::OnDialogReady(WPARAM wParam, LPARAM lParam)
{
    DialogThreadParams* params = (DialogThreadParams*)lParam;
    this->currentID = params->nextDialogID;
    m_shown = FALSE;
    this->ActivateFrame();
    return 0;
}

BOOL SMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    if( !CFrameWnd::PreCreateWindow(cs) )
        return FALSE;

    cs.dwExStyle &= ~WS_EX_CLIENTEDGE | SWP_DRAWFRAME; 
    cs.style  =  WS_EX_CLIENTEDGE; 

    return TRUE;
}

void SMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{

    if(m_shown == FALSE)
    {
        m_shown = TRUE; 
        if(IsWindow(views[currentID]->m_hWnd))
        {
            views[currentID]->SetFocus();
        }
        else
        {
            views[currentID]->DoModal();
        }
    }
    CFrameWnd::OnActivate(nState, pWndOther, bMinimized);
}

The MainMenuDialog is an inherited class from StandardDialog, which is inherited from CDialog. I will post only pieces of the code now, since this post is already too long(If you need more tell me which part can be interesting)... The dialog which offers the possibility to close the app ist called ShutdownDialog(only inherited from CDialog) and is stored as a private variable of MainMenuDialog:

ShutdownDialog* shutdownDialog;

Created in constructor of MainMenuDialog:

this->shutdownDialog = new ShutdownDialog();

I show the dialog on a button click:

void MainMenuDialog::OnClickedShutdownButton()
{
    shutdownDialog->DoModal();
}

And is deleted in destructor of MainMenuDialog:

MainMenuDialog::~MainMenuDialog()
{
    delete shutdownDialog;
}

In ShutdowDialog i close the app with this piece of code:

AfxGetMainWnd()->PostMessage(WM_CLOSE);
EndDialog( 0 );

Until this everything is working fine. The application starts to destroy the objects. But after finishing the call to SMainFrame destructor i get an access violation. The program doesnt stops, just a line in Output window. It continues with statement "return FALSE;" in SWinApp InitInstance().

I know that access violation occurres when deleting an object twice or using a pointer where the depending object was already destroyed, but i cannot figure out whats going wrong here. Additional i have to say that SWinApp and SMainFrame was created by a colleague and i modified the parts with the dialogs in SMainFrame. I thought that the m_pMainWnd could be the problem since after the destructor call to SMainFrame it must be an invalid pointer. So i tried:

SMainFrame::~SMainFrame()
{
    for(auto iterator:views)
    {
        delete iterator.second;
    }
    delete this->configuration;
    delete this->currentData;
    AfxGetApp()->m_pMainWnd = NULL;
}

But the violation still occurres... I searched for the Callstack Window but could not find it under the view tab... Sry for this long post! And sry if this is too specific...But i am struggling since severals hours and have no idea what i can try...Any help is welcome!

4
  • Dont do AfxGetApp()->m_pMainWnd = NULL; The framework does it for you. Mar 25, 2015 at 11:08
  • 1
    Can you run it in Debug mode to see exactly where it crashes? Mar 25, 2015 at 11:12
  • Try changing AfxGetMainWnd()->PostMessage(WM_CLOSE); to ::PostQuitMessage( 0 ); Mar 25, 2015 at 11:19
  • @Andrew Komiagin: Thank you so much! PostQuitMessage solves my problem:) If you post it again as answear i will accept it. Now i only get a message: Warning: Destroying non-NULL m_pMainWnd. But what i read about is, that this is not a fault, just information:)
    – sadermader
    Mar 25, 2015 at 12:29

1 Answer 1

0

Change AfxGetMainWnd()->PostMessage(WM_CLOSE); to ::PostQuitMessage( 0 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.