使用cprintdialog后崩溃 [英] Crash after using cprintdialog

查看:218
本文介绍了使用cprintdialog后崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题发生在64位Windows版本上。如果我运行一个应用程序(在调试模式下构建)调用:

CPrintDialog dlg

dlg.DoModal

然后我选择取消,当我退出从应用程序我在wincore.cpp第415行崩溃

如果应用程序是在发布模式下构建,问题不会发生,但我想将其保持在调试模式。

有什么线索吗?



我的尝试:



DeleteDC(dlg.m_pd.hDC)不起作用,因为它失败

The problem happens on 64 bit Windows version. If I run an application (build in Debug mode) calling:
CPrintDialog dlg
dlg.DoModal
then I select cancel, when I exit from the application I get a crash on wincore.cpp line 415
The problem does not happen if the application is build in release mode, but I would like to keep it in debug mode.
Is there any clue?

What I have tried:

DeleteDC(dlg.m_pd.hDC) does not work because it fails

推荐答案

这不是崩溃,而是抛出一个断言。断言仅在调试版本中抛出。它们主要用于在开发过程中告知您发生了一些奇怪的事情(读:开发人员犯了一个严重错误)。



当提到MFC中的行号时像 wincore.cpp 这样的源文件,你还应该提到使用过的VS版本或者更好的打开那个文件(它位于 VC\atlmfc\src\mfc )并添加功能名称和行​​内容。



使用我的 wincore.cpp 版本,行412到414是

It is not a crash but an assertion is thrown. Assertions are only thrown in debug builds. They are mainly used to inform you during development that something weird has happened (read: the developer has made a serious mistake).

When mentioning a line number within an MFC source file like wincore.cpp, you should also mention the used VS version or better open that file (it is located at VC\atlmfc\src\mfc) and add the function name and the line content.

With my wincore.cpp version the lines 412 to 414 are
CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
ASSERT(pWnd != NULL);					
ASSERT(pWnd==NULL || pWnd->m_hWnd == hWnd);

使用 AfxWndProc()函数。



从调试器运行程序并在发生断言时检查调用堆栈,以了解调用函数的窗口。



另请阅读有关 CPrintDialog类 [ ^ ]并检查您的代码是否适用。因为要在堆栈上创建打印对话框,所以在超出范围时会将其删除。然后,该窗口的断言将更早发生,而不是在关闭应用程序时。因此,断言的真正原因可能是您的应用程序中的其他位置。



为了获得更好的帮助,您应该发布使用打印对话框的完整函数。类似

with the AfxWndProc() function.

Run the program from the debugger and check the call stack when the assertion occurs to know for which window the function is called.

Read also about the CPrintDialog Class[^] and check if your code applies. Because you are creating the print dialog on the stack, it is deleted when going out of scope. Then an assertion for that window would occur earlier and not when closing the application. So the real reason for the assertion might be somewhere else in your application.

For better help you should post the complete function that uses the print dialog. Something like

void SomeClass::SomeFunc()
{
    BOOL bPrintSetupOnly = FALSE;
    CPrintDialog dlg(bPrintSetupOnly);
    if (dlg.DoModal() == IDOK)
    {
        // do something here

        // If dlg.m_pd.hDC has not been assigned to a member variable
        if (!bPrintSetupOnly) // or GetDefaults, or GetPrinterDC has been called
            DeleteDC(dlg.m_pd.hDC);
        // Otherwise do it when not used anymore 
        //  (at least when this SomeClass instance destroyed).
        // But that requires tracking the state (clear member variable after deleting)
        //  and deleting it also on top of this function when not NULL
    }
}


当提供的代码行中发生错误时,可能会出现一些错误。首先:将构造函数中的hDC设置为0以避免任何不必要的调用!!!



尝试:



- 多次删除hDC(检查它只被称为一次

- 对dlg.m_pd的一些错误操作

When the error is happening in the provided code line than are some error causes possible. First: set hDC in the constructor to 0 to avoid any unneeded call!!!

try:

- multiple delete of the hDC (check that it is only called once
- some buggy manipulation of dlg.m_pd
if(dlg.m_pd.hDC !=0) 
{
  DeleteDC(dlg.m_pd.hDC);
  dlg.m_pd.hDC = 0;
  dlg.m_pd = 0;//if possible or somewhere else.
}

认真对待错误,因为错误可能只是更大问题的症状。(我学到了很难)

Take the error seriously because the error may only the symptom of a greater problem. (I learned that the hard way)


我正好同样的问题。当调用CPrintDialog.DoModal()或CPrintDialog.GetDefaults()时,断言会在应用程序退出时触发。这发生在最初在VC6中开发的代码库中(工具et v60)。使用VS2010(工具集v100)构建代码时会出现问题。
I'm having exactly the same problem. When CPrintDialog.DoModal() or CPrintDialog.GetDefaults() is called, then the assertion fires when the application exits. This is happening in a code base that was originally developed in VC6 (toolset v60). The problem arises when the code is build with VS2010 (toolset v100).


这篇关于使用cprintdialog后崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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