2

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; }
IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
Frank
  • 5,741
  • 4
  • 28
  • 49
  • 1
    Did you may recently update Qt libs? May wrong version of library is being used. Try removing (renaming) any probable collision libraries temporarely. – Sebastian Lange Feb 25 '15 at 09:22
  • 1
    `this->name = name;`? Maybe compiler misunderstood what you intended to do there, because `name`, `this->name` and local variable `name` collision? – vahancho Feb 25 '15 at 09:48
  • @vahancho: tried changing that before already without luck... – Frank Feb 25 '15 at 09:54
  • @sebastian: no updates recently... – Frank Feb 25 '15 at 09:55
  • 1
    Try commenting `this->name = name;` and see if problem persists. Also, is `this` deleted before getJourneyName() gets called? – fxam Feb 25 '15 at 09:59
  • 1
    @Frank, the same for `this->id = id;`. Try to comment it as well as `this->name = name`. – vahancho Feb 25 '15 at 10:01
  • same issue ... I end up here as error: file qstirng.h: { Q_ASSERT(&other != this); d->ref.ref(); } – Frank Feb 25 '15 at 10:29
  • 1
    It could be your object got deleted before getName() is called. Example: `Bar *b = new Bar; b->setName("Katie"); delete b; qDebug() << b->getName();` – fxam Feb 25 '15 at 11:02
  • 1
    To check the object deletion sequence, output something in destructor and in getName() and see the order of output. Example: `~Bar() { qDebug() << "dtor()"; } QString getName() { qDebug() << "getName()"; return name; }`. If the output is dtor() then getName(), then the object is used after deleted. Or you can just put breakpoints in dtor and getName() and see who gets called first. – fxam Feb 25 '15 at 11:11
  • @fxam, no not destructed/deleted... – Frank Feb 25 '15 at 11:26
  • 1
    What about commenting code inside setName? `void setName(QString setTo) { //name = setTo; }` Does that segfault happen too? – fxam Feb 25 '15 at 11:28
  • @fxam, yes still happens. when breaking at the getName line, I can see the value is set to the name variable. Fu**** QT just doens't want to return it ;-) – Frank Feb 25 '15 at 12:08
  • 1
    Is your class QObject-derived? Does it have non-default ctor? – Matt Feb 25 '15 at 13:20
  • 2
    I guess you need to provide a [minimal reproducible code](http://sscce.org/) here :) – fxam Feb 25 '15 at 13:43
  • 1
    could try separating the implementation out of the header and see if the same error occurs? – AngryDuck Feb 25 '15 at 17:19

2 Answers2

2

I was facing similar issue. Although I could not find the root cause of this issue, I have another observation. If we define the getter functions outside the class declaration using scope resolution operator the code works.

QString MyClass::GetX(void) {
    return mX;
}

QString MyClass::GetY(void) {
    return mY;
}

class MyClass
{
public:
    MyClass(){}

    /* Functions for setting mX and mY strings. */

    QString GetX(void);

    QString GetY(void);

    isDataAvailable()
    {
        return mAvailable;
    }

private:
    bool mAvailable;
    QString mX;
    QString mY;
};

As I understand, in C++, if we define a function within class declaration, by default it is inline... so the issue could be something related with inlining of the functions.

AVD
  • 41
  • 1
  • 7
  • I think [AngryDuck](https://stackoverflow.com/users/1110338/angryduck) suggested same solution... For me it works. Saw the comment later. – AVD Apr 09 '18 at 12:09
1

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; }
Frank
  • 5,741
  • 4
  • 28
  • 49