Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Segfault with QMap
QtWS25 Last Chance

[solved] Segfault with QMap

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LonniB
    wrote on last edited by
    #1

    Hello everyone,

    I've been trying to find out where my problem could be for hours now and I thought maybe I could get some help from you all.

    So I have a class that goes like this:
    Header File (note that the mother class has all the same methods and attributes)
    @#ifndef HMITIMERADOBJECT_H
    #define HMITIMERADOBJECT_H

    #include "hmigenericobject.h"
    #include "hmigenericparam.h"
    #include <QString>
    #include <QObject>
    #include <QMap>

    class HmiTimerADObject:public HmiGenericObject
    {
    Q_OBJECT
    public:
    explicit HmiTimerADObject(QString name, int criticity = 0, QObject *parent = 0);
    explicit HmiTimerADObject(QString name, QString duration, int criticity = 0, QObject *parent = 0);

    QString getName(void) { return m_name; };
    
    virtual void show(void);
    virtual void hide(void);
    
    virtual void addParam(HmiGenericParam * pParam);
    virtual HmiGenericParam * getParam(QString name);
    
    void syncParam(void);
    
    void setADDuration(QString duration);
    

    private:
    QMap <QString, HmiGenericParam *> m_paramMap;
    };

    #endif // HMITIMERADOBJECT_H
    @

    cpp file :

    @#include "hmitimeradobject.h"

    #include <QDebug>
    #include <hmigenericparam.h>

    HmiTimerADObject::HmiTimerADObject(QString name, int criticity, QObject *parent):
    HmiGenericObject(name,criticity,parent)
    {
    HmiGenericParam x (QString("x"),QString("50")) ;
    HmiGenericParam y (QString("y"),QString("10")) ;
    HmiGenericParam initialTime (QString("initialTime"),QString("0")) ;
    this->addParam(&x);
    this->addParam(&y);
    this->addParam(&initialTime);
    }

    HmiTimerADObject::HmiTimerADObject(QString name, QString duration, int criticity, QObject *parent):
    HmiGenericObject(name,criticity,parent)
    {
    HmiGenericParam x (QString("x"),QString("50")) ;
    HmiGenericParam y (QString("y"),QString("10")) ;
    HmiGenericParam initialTime (QString("initialTime"),duration) ;
    this->addParam(&x);
    this->addParam(&y);
    this->addParam(&initialTime);

    qDebug()<< " Value of initialTime ---&gt; " << this->getParam("initialTime")->getValue() ;
    

    }

    void HmiTimerADObject::setADDuration(QString duration){
    Q_ASSERT(this->m_paramMap.contains(QString("initialTime")));
    //qDebug()<< "Test :: " << m_paramMap ;
    QMap<QString, HmiGenericParam *>::iterator i;
    for (i = this->m_paramMap.begin(); i != this->m_paramMap.end(); ++i){
    qDebug() << i.key() << ": " ;
    }
    qDebug()<<this->getParam("initialTime")->getValue();
    }

    /**************************************************************************

    • getParam
    • This method retrieves a parameter class by parameter namespace
      */
      HmiGenericParam * HmiTimerADObject::getParam(QString name)
      {
      return m_paramMap.value(name);
      }@

    But the thing is that when I call :
    @ HmiTimerADObject timerAD (QString("timer"), QString("1260"));
    timerAD.setADDuration(QString("40000"));@

    I get the following application output with afterwards a SIGSEGV :
    @Test 2 :: "50"
    HmiTimerADObject::addParam ffffd6a0 y
    Test 2 :: "10"
    HmiTimerADObject::addParam ffffd6c0 initialTime
    Test 2 :: "1260"
    Value of initialTime ---> "1260"
    "initialTime" :
    "x" :
    "y" :
    @
    Can anyone give me some hints about it all?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      In your constructor, you are creating HmiGenericParam objects on a stack. That means they are deleted when the scope ends (when the constructor ends). So the pointers you pass to addParam() become dangling (invalid).

      When you then try to access them, the program crashes, which is to be expected. You should either change your proegram structure (why use pointers here at all?), or create those HmiGenericParam objects on heap (with "new").

      (Z(:^

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LonniB
        wrote on last edited by
        #3

        Ok, thank you very much for helping me through this. I can't think of why I did not see that before. Thanks a lot :)

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          You are welcome. Don't worry, this kind of thing happens regularly to everybody :-)

          (Z(:^

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved