Why my code crash?



  • Hi, I have such code:

    class MyString {
    public:
    getStr(){ 
    if(str.endsWith("abc")) return "abc";
    else return str;
    }
    
    private:
    QString str;
    }
    
    class Par {
    MyString &getStr(){retrun mystr;}
    private:
    MyString mystr;
    }
    

    in one thread, I have Par * par = new Par;
    and then use signal/slot to put par to another thread, another thread calls par->getStr.getStr(), which leads to segmentation fault at "if(str.endsWith("abc")) return "abc"";.
    I can not undersand it.
    What is wrong?

    Thanks everyone!


  • Moderators

    @Mr-Pang Segmentation fault most probably means that par is null pointer or is not pointing to a valid object. Can you show the code where you emit the signal and the slot also?
    And I guess you have a typo here, right?

    par->getStr.getStr()
    

    it should be

    par->getStr().getStr()
    


  • Hi,

     Session *s = new Session(mw->datamgr, QString("apn"), 1);
        qWarning()<<__FUNCTION__<<s;
        s = new Session(mw->datamgr, QString("apn"), 1);
        qWarning()<<__FUNCTION__<<s;
        s = new Session(mw->datamgr, QString("apn"), 1);
        qWarning()<<__FUNCTION__<<s;
        s = new Session(mw->datamgr, QString("apn"), 1);
        qWarning()<<__FUNCTION__<<s;
        s = new Session(mw->datamgr, QString("apn"), 1);
        qWarning()<<__FUNCTION__<<s;
    

    I got

    handleCsreqMsg Session(0x7fffd0003a00)
    handleCsreqMsg Session(0x7fffd0003220)
    handleCsreqMsg Session(0x7fffd0003a00)
    handleCsreqMsg Session(0x7fffd0003220)
    handleCsreqMsg Session(0x7fffd0003a00)
    

    I continuous create 5 Session object, but the pointer is alternate 0x7fffd0003a00 and 0x7fffd0003220
    Why so strange?



  • What is Session?



  • class Session : public QObject
    {
        Q_OBJECT
    public:
    explicit Session(DataMgr *mgr, const QString &_apn, quint64 _imsi);
    private:
        Imsi imsi;
        Apn apn;
        quint32 seq;
        FTeid l_fteid_c, r_fteid_c;
        Paa paa;
        ULI uli;
        ServingNetwork servingnetwork;
        Ambr ambr;
        RatType rat;
        ChargingCharacteristics cc;
        QList<Bearer*> bearers;
        packetCounter counter;
        Stat stat;
        DataMgr *datamgr;
    }
    Session::Session(DataMgr *mgr, const QString &_apn, quint64 _imsi)
        : datamgr(mgr), stat(INACTIVE)
    {
        if(_imsi){
            imsi.setImsi(_imsi);
            apn.setApn(imsi, _apn);
        }
        quint32 lteid = GtpUtil::getTeidC();
        Bearer *b = new Bearer(this);
        b->getLFTeidU().setTeid(lteid);
        l_fteid_c.setTeid(lteid);
        bearers.append(b);
        datamgr->addBearer(lteid, b);
        datamgr->addSession(_apn, _imsi, this);
    
    }
    


  • Oh, no. I delete it myself. I am noob


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.