[solved] Segfault with QMap
-
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 ---> " << 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? -
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").
-
You are welcome. Don't worry, this kind of thing happens regularly to everybody :-)