문제

I am testing this library but I am getting a segmentation fault whenever it reachs a certain line (the commented one below). This issue comes from this question - tldr the same problem on a much bigger project, so I decided to test the libraries separatedly and apparently this is what fails. This code works on a co-worker's 32bits machine using Qt4 (he handed me the code). I migrated it to Qt5 and compiled with a 32bit compiler and I am getting the segmentation fault. If I comment the offending line and the two below it the program runs (although its just an empty window).
What could be happening?

#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;
}

Debug info: enter image description here

PS: What does it mean that some locals & expressions are in red?

EDIT

Alright, the only major changes I had to make in the library code were these:

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);
            }
        }

To this (there are more ifs but this illustrates it properly)

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);
            }
        }

In theory it should be the same shouldnt it? Although I do find strange that in the first snippet it uses -> instead of . when its not a pointer. I had to change it to that because I was getting these errors:

                          ^
..\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");
                                                                ^
도움이 되었습니까?

해결책

In your changed code you have the line

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

But if "POI0" is not found the find function would return end which is an iterator pointing to beyond the collection, and so it's value function would be causing undefined behavior.


As for the errors it seems that the QMap object is constant, and so you can't get non-const iterators. Just change to use AttributeSet::const_iterator instead and you can use the original function otherwise unmodified. This will probably fix your crashes, as then you don't have the risk of undefined behavior as described above.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top