MFC c++ 奇怪的问题

发布于 2024-10-11 11:30:18 字数 407 浏览 6 评论 0原文

我正在 MFC /c++ 中制作一个简单的应用程序。这段代码是在 CWinApp 类中创建一个对话框。它可以正常编译并且也可以正常运行,但前提是我从 VStudio 运行它。但是如果我直接运行它,则会发生运行时错误并编程坠毁了。

CMyDialog dlg;
m_pMainWnd = (CWnd*)&dlg;
dlg.DoModal();

但如果我使用下面的代码,那么一切都很好。我无法理解这种行为。

CMyDialog *dlg=new CMyDialog();
m_pMainWnd = (CWnd*)dlg;
dlg->DoModal();

在我之前的其他一些 C++(非 MFC)项目中,这种类型的事情也发生过很多次。 请向我指定这一点。

I am making an simple application in MFC /c++.This code is to ceate a dialog in CWinApp class.It compile fine and also run fine but only if i run it from VStudio.But if i run it directly,runtime error occured and program crashed.

CMyDialog dlg;
m_pMainWnd = (CWnd*)&dlg;
dlg.DoModal();

but if i used the code just below,then everything is fine.I could't understand this behaviour.

CMyDialog *dlg=new CMyDialog();
m_pMainWnd = (CWnd*)dlg;
dlg->DoModal();

This type of thing were happened many times before also for me in some other past C++(non MFC) projects.
please specify this to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佼人 2024-10-18 11:30:18

在第一种情况下,对象被销毁(当 dlg 超出范围时),但仍由 m_pMainWnd 指向。有可能/很可能是某些原因导致 m_pMainWnd 被使用并访问不再存在的对象。

将第一个示例更改为:

CMyDialog dlg;
m_pMainWnd = (CWnd*)&dlg;
dlg.DoModal();
m_pMainWnd = NULL;

如果这解决了问题,那么在第二种情况下您逃脱的原因是该对象没有被销毁(尽管由于 m_pMainWnd 指向它,所以当程序关闭时 MFC 可能会为您销毁它;事实上,这可能是另一种情况下崩溃的根源)。

In the first case the object is being destroyed (when dlg goes out of scope) but is still pointed to by m_pMainWnd. It's possible/likely that something is causing m_pMainWnd to be used and accessing an object which no longer exists.

Change the first example to:

CMyDialog dlg;
m_pMainWnd = (CWnd*)&dlg;
dlg.DoModal();
m_pMainWnd = NULL;

If that solves the problem, the reason you got away with it in the second case is that the object was not destroyed (although since m_pMainWnd points to it, MFC may be destroying it for you when the program shuts down; indeed, that may be the source of the crash in the other case).

苍景流年 2024-10-18 11:30:18

这两个代码块之间的主要区别在于,在第一个代码块中,dlg 将在代码块末尾被销毁,而在第二个代码块中,它不会在此处的任何代码中被销毁。

考虑到程序的其余部分,在其声明的块的末尾是否仍然需要 dlg ?如果是这样,您需要使用第二个块之类的东西。

或者,DoModal 可能会调用 delete(this)(直接或间接)——在这种情况下,dlg 必须使用 新。

The main difference between these two code blocks is that in the first, dlg is going to be destroyed at the end of the block, and in the second, it's not destroyed in any code here.

Given the rest of your program, is dlg still needed at the end of the block it's declared in? If so, you need to use something like the second block.

Alternatively, it's possible that DoModal calls delete(this) (directly or indirectly) -- in that case, dlg must be made with new.

街角卖回忆 2024-10-18 11:30:18

问题是您将 m_pMainWnd 设置为一个窗口,当对话框关闭时该窗口将不再存在,并且这通常发生在对话框销毁之前,就在您关闭对话框时(当DoModal返回时)。

我通过创建一个虚拟窗口并将其用作主窗口解决了这个问题:

class CDummyWindow : public CFrameWnd
{
public:
    CDummyWindow()
    {
        Create(NULL, NULL);
    }
};

Class::InitInstance()
{
  ...
  CDummyWindow win;
  m_pMainWnd = &win;
  ...
  return FALSE;
}

请确保您返回 FALSE 指示应用程序应该停止。

The problem is that you set m_pMainWnd to a window which will not exist anymore when the dialog is closed, and this typically happens before the dialog destruction, just when you close the dialog (when the DoModal returns).

I resolved this problem by creating a dummy window and using it as the main window:

class CDummyWindow : public CFrameWnd
{
public:
    CDummyWindow()
    {
        Create(NULL, NULL);
    }
};

Class::InitInstance()
{
  ...
  CDummyWindow win;
  m_pMainWnd = &win;
  ...
  return FALSE;
}

Be sure that you return FALSE indicating that the app should stop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文