Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Crash with QMap as an argument



  • Hi, I’ve a problem with QMap as an argument passed to a class.
    I’ve 2 classes (MainWindow and ProcessData).
    In MainWindow.h I define the QMap as follow:

    QMap<uint16_t, QString> *m;
    

    In MainWindow constructor I fill my QMap with:

    m = new QMap<uint16_t, QString>;
    m->insert(0, “String0”);
    m->insert(1, “String1”);
    m->nsert(2, “String2”);
    m->insert(3, “String3”);
    

    Using “m” in MainWindow there isn’t a problem.

    The problem is when I give this QMap (m) to constructor of class ProcessData:
    In header, I declare the constructor as follow:

    ProcessData(QWidget *parent, QMap<uint16_t, QString> *LangMsg);
    

    and QMap:

    QMap<uint16_t, QString> *p;
    

    In .cpp file o class, the constructor is defined:

    ProcessData::ProcessData(QWidget *parent, QMap<uint16_t, QString> *LangMsg) : QWidget(parent)
    {
         p = LangMsg;
    }
    

    If I use “p” to extract string in any widget or object (for example QString s = p->value(0)) I haven’t a problem, but the program crash to this line:

    ParamTable->setHorizontalHeaderLabels(QStringList() << p->value(0) << p->value(1) << p->value(2) << p->value(3));
    

    If I declare and fill a fake *QMap p in class ProcessData(), there isn't a problem.
    I don’t understand why!
    Can you help me?
    Thanks.



  • @Stefanoxjx said in Crash with QMap as an argument:

    but the program crash to this line:

    Please start with a Q_ASSERT(p)! Just in case.... You haven't even shown us your call to ProcessData().

    Nor, for that matter, your declaration of p. Which I sort of assume is a member variable? Recently people have been declaring member variables and then "shadowing" them with local variables using the same name, and wondering why they don't have the value of the member variable --- you're not in that situation are you?



  • Excuse me, I'm a stupid.
    In reality, the critical code line is called in another class :(
    Sorry.


Log in to reply