gpt4 book ai didi

c++ - 测试从 qt4 迁移到 qt5 的库时出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:35 24 4
gpt4 key购买 nike

我正在测试这个库,但每当它到达某一行(下面评论的那一行)时,我就会遇到段错误。本期来自this question - tldr 在一个更大的项目上遇到同样的问题,所以我决定分别测试这些库,显然这就是失败的原因。这段代码在同事的 32 位机器上使用 Qt4 运行(他把代码交给了我)。我将它迁移到 Qt5 并使用 32 位编译器编译,但我遇到了段错误。如果我评论有问题的行和它下面的两行,程序就会运行(尽管它只是一个空窗口)。
会发生什么?

#include "qenctest.h"

#include <QLibrary>
#include <QtWidgets/QMessageBox>

typedef void (*encRefresh)(QPainter*);
encRefresh enc_refresh = NULL;

typedef void (*encResize)(QSize);
encResize enc_resize = NULL;

typedef QENCSignaler* (*encInit)(QString);
typedef void (*encOpenFile)(QString);

QENCTest::QENCTest(QWidget *parent, Qt::WindowFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

QLibrary _qenc("qenc");
encInit enc_init;
encOpenFile enc_openFile;

enc_init = (encInit) _qenc.resolve("init"); // I checked and it does load the library and the symbol succesfully
enc_openFile = (encOpenFile) _qenc.resolve("openFile");
enc_resize = (encResize) _qenc.resolve("resize");
enc_refresh = (encRefresh) _qenc.resolve("refresh");

QString path = "encfg";
QENCSignaler* qencSignaler = enc_init(path); // Throws segfault here

connect(qencSignaler, SIGNAL(newChart(Chart*)), this, SLOT(qencNewChart(Chart*)));
connect(qencSignaler, SIGNAL(startReadChart(char*)), this, SLOT(qencStartReadChart(char*)));

enc_openFile("PL2BAPOL.000");

int _s = 0;
}

调试信息: enter image description here

PS:有些locals & expression是红色的是什么意思?

编辑

好吧,我必须在库代码中进行的唯一主要更改是:

AttributeSet::iterator vItPOI = attributes.at(i).find("POI");
if (vItPOI == attributes.at(i).end()) continue;
AttributeSet::iterator vItPOI0 = attributes.at(i).find("POI0");
if (vItPOI0 == attributes.at(i).end()) continue;
if (vItPOI -> getStringValue() == "Bankowoæ" &&
selectedPOI & POI_BANKING) {

if (vItPOI0 -> getStringValue() == "Placówka banku") {

drawSymbol(painter, x, y, POI_BANKING);
}
}

为此(还有更多的如果,但这正确地说明了这一点)

ShapeAttribute vItPOI = attributes.at(i).find("POI").value();
if (attributes.at(i).find("POI") == attributes.at(i).end()) continue;
ShapeAttribute vItPOI0 = attributes.at(i).find("POI0").value();
if (attributes.at(i).find("POI0") == attributes.at(i).end()) continue;
if (vItPOI . getStringValue() == "Bankowo��" &&
selectedPOI & POI_BANKING) {

if (vItPOI0 . getStringValue() == "Plac�wka banku") {

drawSymbol(painter, x, y, POI_BANKING);
}
}

理论上应该是一样的吧?尽管我确实觉得奇怪,在第一个片段中它使用 -> 而不是 .当它不是指针时。我不得不将其更改为那个,因为我遇到了这些错误:

                          ^
..\qenc\ShapeLandPOI.cpp: In member function 'virtual void ShapeLandPOI::draw(QPainter*)':
..\qenc\ShapeLandPOI.cpp:74:62: error: conversion from 'QMap<QString, ShapeAttribute>::const_iterator' to non-scalar type 'QMap<QString, ShapeAttribute>::iterator' requested
AttributeSet::iterator vItPOI = attributes.at(i).find("POI");
^
..\qenc\ShapeLandPOI.cpp:76:64: error: conversion from 'QMap<QString, ShapeAttribute>::const_iterator' to non-scalar type 'QMap<QString, ShapeAttribute>::iterator' requested
AttributeSet::iterator vItPOI0 = attributes.at(i).find("POI0");
^

最佳答案

在你修改后的代码中有一行

ShapeAttribute vItPOI0 = attributes.at(i).find("POI0").value();

但是如果 "POI0"未找到 find函数将返回 end这是一个指向 beyond 集合的迭代器,所以它是 value函数会导致 undefined behavior .


至于错误似乎是 QMap object 是常量,所以你不能得到非常量迭代器。只需更改为使用 AttributeSet::const_iterator相反,您可以使用未经修改的原始功能。这可能会修复您的崩溃,因为这样您就不会面临上述未定义行为的风险。

关于c++ - 测试从 qt4 迁移到 qt5 的库时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23654195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com