无法创建主窗口? [英] Cannot create main window?

查看:260
本文介绍了无法创建主窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图想出这个数小时,我在我的机智的尽头。



在找到一些C ++ MFC教程之后,我试图用C ++编写一个简单的GUI应用程序,但它似乎有问题,在生成主窗口(对话框?)。当我尝试编译代码时,以下消息显示:


GUI_Employee_0501.exe中0x00E7A9DC处的未处理异常:
0xC0000005 :访问冲突读取位置0xFEFEFF66。


,断点停止在 winmain.cpp > pThread-> m_pMainWnd-> DestroyWindow(); 。 pThread-> m_pMainWnd 的值为 NULL ,我怀疑是导致此问题的原因。



你指定这里的问题是什么?我有一个示例代码,这与我非常相似,但是工作,所以我真的不知道发生了什么!

  #include< afxwin.h> 
#includeresource.h
#include< iostream>
using namespace std;

#pragma注释(链接器/ entry:WinMainCRTStartup / subsystem:console)

CEdit * pFNAME;
CEdit * pLNAME;
CEdit * pSALARY;
CEdit * pDDAY;
CEdit * pMMONTH;
CEdit * pYYEAR;

CComboBox * pGENDER;

CButton * pADD;
CButton * pDELETE;
CButton * pSAVE;
CButton * pLOAD;

class ENTRY_FORM:public CDialog
{
public:
ENTRY_FORM(CWnd * pParent = NULL):CDialog(ENTRY_FORM :: IDD,pParent){}
enum {IDD = dialog_main};
protected:
virtual void DoDataExchange(CDataExchange * pDX){CDialog :: DoDataExchange(pDX); }

virtual BOOL OnInitDialog()
{
CDialog :: OnInitDialog();
SetUpInterfacePointers();
return true;
}

void SetUpInterfacePointers(){...有自己的东西...}

public:
afx_msg void add(){PRESS_ADD (); }

void PRESS_ADD(){...有自己的东西...}

DECLARE_MESSAGE_MAP()
};

类程序:public CWinApp
{
public:
Program(){}

public:
virtual BOOL InitInstance ()
{
CWinApp :: InitInstance();
cout<< CWinAPP:initInstance< endl;
ENTRY_FORM dlg;
m_pMainWnd =& dlg;
cout<< mpMainWnd< endl;
INT_PTR nResponse = dlg.DoModal();
return FALSE;
}
};

//
BEGIN_MESSAGE_MAP(ENTRY_FORM,CDialog)
ON_COMMAND(button_add,add)
END_MESSAGE_MAP()
//
$ b b编程应用程序;

解决方案

  return FALSE; 

  return TRUE; 

这是因为 CWinApp :: InitInstance 应该在失败的情况下返回FALSE,如果所有初始化都正常,则返回TRUE。如果失败, AfxWinMain 将尝试销毁 m_pMainWnd 指向的窗口,这是不可能的(它导致未定义的行为),因为你分配到 m_pMainWnd 一个局部对象(一旦InitInstance结束就被销毁)。



/ p>

SB Bae - 要进一步调查并找到根本原因,您需要调试地方,其中m_pMainWnd应在对话结束后设置为NULL。这应该在wincore.cpp中的 CWnd :: OnNcDestroy()中。有一行 pThread-> m_pMainWnd = NULL; 显然没有在您的应用程序中执行。


I've been trying to figure this out for hours now, and I'm at my wit's end. I would surely appreciate it if someone could tell me when I'm doing wrong.

After finding some C++ MFC tutorials, I tried to make a simple GUI application with C++, but it seems to have problem at generating the main window(dialog?). When I'm trying to compile the code, following message shows :

Unhandled exception at 0x00E7A9DC in GUI_Employee_0501.exe: 0xC0000005: Access violation reading location 0xFEFEFF66.

and the break point stops inside winmain.cpp, at pThread->m_pMainWnd->DestroyWindow();. The value of pThread->m_pMainWnd is NULL, which I suspect as the cause of the problem.

Can you specify what is the problem here? I have a sample code and that is very similar to mine but that works, so I truly have no idea what is happening!

#include <afxwin.h>
#include "resource.h"
#include <iostream>
using namespace std;

#pragma comment(linker, "/entry:WinMainCRTStartup /subsystem:console")

CEdit *pFNAME;
CEdit *pLNAME;
CEdit *pSALARY;
CEdit *pDDAY;
CEdit *pMMONTH;
CEdit *pYYEAR;

CComboBox *pGENDER;

CButton *pADD;
CButton *pDELETE;
CButton *pSAVE;
CButton *pLOAD;

class ENTRY_FORM : public CDialog
{
public:
    ENTRY_FORM(CWnd* pParent = NULL) : CDialog(ENTRY_FORM::IDD, pParent) { }
    enum { IDD = dialog_main };
protected:
    virtual void DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); }

    virtual BOOL OnInitDialog()
    {
        CDialog::OnInitDialog();
        SetUpInterfacePointers();
        return true;
    }

    void SetUpInterfacePointers(){ ... does its own thing ... }

public:
    afx_msg void add() { PRESS_ADD(); }

    void PRESS_ADD() { ... does its own thing ... }

    DECLARE_MESSAGE_MAP()
};

class Program : public CWinApp
{
public:
    Program(){ }

public:
    virtual BOOL InitInstance()
    {
        CWinApp::InitInstance();
        cout << "CWinAPP:initInstance" << endl;
        ENTRY_FORM dlg;
        m_pMainWnd = &dlg;                  
        cout << "mpMainWnd" << endl;
        INT_PTR nResponse = dlg.DoModal();  
        return FALSE;
    }
};

//
BEGIN_MESSAGE_MAP(ENTRY_FORM, CDialog)
    ON_COMMAND(button_add, add)
END_MESSAGE_MAP()
//

Program theApp;

解决方案

Change in InitInstance():

return FALSE;

to

return TRUE;

This is because CWinApp::InitInstance should return FALSE only in case of failure, and TRUE if all initialization went OK. In case of failure, AfxWinMain will try to destroy window pointed by m_pMainWnd, which is not possible (it causes Undefined Behaviour) because you assign to m_pMainWnd a local object (which is destroyed once InitInstance ends).

[edit]

S.B Bae - to investigate it further and find a root cause, you will need to debug place where m_pMainWnd should be set to NULL once your dialog ends. This should be in CWnd::OnNcDestroy() in wincore.cpp. There is a line pThread->m_pMainWnd = NULL; which apparently is not being executed in your application.

这篇关于无法创建主窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆