首页 > 解决方案 > 从 GDCM 分配字符串时出现异常

问题描述

我有一个使用较新版本的 VC++ 和 GDCM 构建的 Qt 应用程序。我之前使用 VC++ 2015 和旧版本的 GDCM 构建了应用程序,它编译并运行得很好。现在我在字符串赋值中遇到了一个异常。

gdcm::Reader r;
r.SetFileName(f.toStdString().c_str());
if (r.Read()) {
    gdcm::StringFilter sf;
    sf = gdcm::StringFilter();
    sf.SetFile(r.GetFile());
    std::string s;

    /* get modality */
    gdcm::Tag tag = gdcm::Tag(0x0008,0x0060);
    s = sf.ToString(tag); // <-- runtime error here...
    fileModality = QString(s.c_str());

    /* get patientID */
    s = sf.ToString(gdcm::Tag(0x0010,0x0020));
    filePatientID = QString(s.c_str());

    /* get protocol (seriesDesc) */
    s = sf.ToString(gdcm::Tag(0x0008,0x103E));
    fileProtocol = QString(s.c_str());
}

如果我使用该assign函数,则错误将移至下一行,即从字符串到 c_str 的转换。

s.assign(sf.ToString(tag));
fileModality = QString(s.data());

我不确定发生了什么,但这似乎是 GDCM 没有正确返回字符串对象的问题。

编辑:错误是一个带有以下内容的对话框

The inferior stopped because it triggered an exception.
Stopped in thread 0 by: Exception at 0x7ffaa7b8f621, code: 0xc0000005: read access violation at: 0xfffffffffff, flags=0x0 (first chance).

还有调用栈,从我自己的函数调用开始

1   std::_Container_base12::_Swap_all                                                 xutility           239  0x7ff7764a7b7a 
2   std::_String_alloc<std::_String_base_types<char,std::allocator<char>>>::_Swap_all xstring            2029 0x7ff7764a7af7 
3   std::string::_Assign_rv_contents_with_alloc_always_equal                          xstring            2353 0x7ff7764a619d 
4   std::string::_Assign_rv_contents                                                  xstring            2326 0x7ff7764a6132 
5   std::string::operator=                                                            xstring            2308 0x7ff7764a37cd 
6   MainWindow::GetFileType                                                           mainwindow.cpp     477  0x7ff77648784c 
... <More>                                                                                                                   

标签: c++qtgdcm

解决方案


我把这个放在这里,以防有人遇到类似的问题。

我能够找出问题所在。GDCM 是在 Release 中编译的……但我的应用程序是在 Debug 中编译的。不知何故,它链接得很好并且大部分时间都在运行,但是当它遇到来自 GDCM 库的函数调用时,它崩溃了。在 Release 或 Debug 中编译所有内容修复了它。


推荐阅读