【问题标题】:Second use of CFileDialog in my program gets the run-time error Debug Assertion failed在我的程序中第二次使用 CFileDialog 得到运行时错误 Debug Assertion failed
【发布时间】:2013-09-04 16:47:22
【问题描述】:

我用 MFC 开发了一个简单的程序。它负责使用 GDAL 库读取和写入 geotiff 文件。为此,我从CFileDialog 派生了两个类,名为ManageOpenGeoTiffFilesManageSaveGeoTiffFiles,每个类都有3 个函数来支持读写geotiff。

这是其中之一的构造函数和析构函数的标题:

ManageOpenGeoTiffFiles::ManageOpenGeoTiffFiles(void):CFileDialog(true,0,0,OFN_ENABLESIZING | OFN_HIDEREADONLY,_T("Tiff Files (*.tif)|*.tif|"),0,0,true)  

ManageOpenGeoTiffFiles::~ManageOpenGeoTiffFiles(void)
{
}

这就是我在代码中使用它的方式:

void CInitialJobProject2FinalDlg::OnBnClickedBtnopen()
{
// TODO: Add your control notification handler code here
m_oglWindow1.WantToPan = false;
m_oglWindow1.WantToUseZoomTool = false;
CString fullpath;
if ( m_openFiles.DoModal() == IDOK )
{   
    fullpath = m_openFiles.GetPathName();
    try{
        m_openFiles.OpenGeoTiffAsReadonly(fullpath);
    }
    catch(CFileException *e){
        MessageBox(_T("the file could not be opened"),_T("error"),MB_OK);
        this ->ExitMFCApp();
    }
    m_openFiles.ReadRasterData();  
}
else
    MessageBox(_T("you pressed cancel and can not proceed."),_T("error"),MB_ICONERROR);
}  

当我在我的程序中第一次使用OpenSave 按钮时一切正常,但在第二次使用时出现错误:


如果我点击忽略:

此错误发生在以下行:

if ( m_openFiles.DoModal() == IDOK )  

每个对话框,即使我第一次单击cancel,在第二次使用对话框时也会出现错误。

dlgFile.cpp第398行如下:

hr = (static_cast<IFileDialog*>(m_pIFileDialog))->SetFileTypes(nFilterCount, pFilter);
            ENSURE(SUCCEEDED(hr));  

编辑部分:

回答其中一个 cmets 并为其他人提供信息:

当我设置断点时,断言失败时会看到这些结果:

pFilter 0x00fc3660 {pszName=0x00fc36a8 "Tiff Files (*.tif)" pszSpec=0x00fc3788 "*.tif" }  
hr  E_UNEXPECTED  

第一次断言不失败时的结果如下:

pFilter 0x004cfca0 {pszName=0x004cfce8 "Tiff Files (*.tif)" pszSpec=0x004cfdc8 "*.tif" }  
hr  S_OK

【问题讨论】:

  • 向我们展示 pFilter 指向的内容。一旦 ASSERT 发生,hr 的值是多少?
  • @Michael Walz 我已经回复了您的评论,请参阅已编辑部分

标签: c++ mfc cfiledialog


【解决方案1】:

您将格式错误的过滤器字符串传递给CFileDialog::CFileDialog。备注部分陈述了以下条件:

lpszFilter 参数[...] 以两个 '|' 字符结尾。

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 2020-11-18
    相关资源
    最近更新 更多