Search code examples
c++qtsegmentation-faultqt4.8

Segmentation fault when getting QString


Strange problem, already looked into with several colleagues... Using Qt Creator and Qt 4.8.5

  • define an object
  • set a number of values with setters
  • request values with a getters
  • result: getting an int no problem, all other values give segmentation fault
  • but with breakpoint in debugger the values are correctly shown, so they are in the object!
  • same code worked before, problem "just appeared". Compiler issue?

    private:
        int id;
        QString name;
    
    public;
       int getId() { return this->id; } // OK
       void setId(int id) { this->id = id; } 
    
       QString getName() { return this->name; } // SIGSEGV
       void setName(QString name) { this->name = name; }
    

Any ideas? Same issue known?

UPDATE

Changed code to this, based on comments, still same issue

    private:
        int id;
        QString name;

    public;
       int getId() { return id; } // OK
       void setId(int setTo) { id = setTo; } 

       QString getName() { return name; } // SIGSEGV
       void setName(QString setTo) { name = setTo; }

Solution

  • thinking further about the way objects are created in memory, I thought that a QString maybe doesn't reserve fixed number of bytes, which could be the cause of this strange behavior and guess what, a dummy change solved my problem...

    This feels like a really "dirty" solution, but at least I can go on with my work ;-) But any idea's on the root cause would really be appreciated! Thanks already for all the valuable comments!!!

    private:
        QString name; // FIRST DEFINE QSTRING
        int id; // THEN DEFINE INT
    
    public;
       int getId() { return id; } // OK
       void setId(int setTo) { id = setTo; } 
    
       QString getName() { return name; } // OK
       void setName(QString setTo) { name = setTo; }