简体   繁体   English

获取QString时出现分段错误

[英]Segmentation fault when getting QString

Strange problem, already looked into with several colleagues... Using Qt Creator and Qt 4.8.5 奇怪的问题,已经与几位同事一起考虑过......使用Qt Creator和Qt 4.8.5

  • define an object 定义一个对象
  • set a number of values with setters 使用setter设置多个值
  • request values with a getters 使用getter请求值
  • result: getting an int no problem, all other values give segmentation fault 结果:得到一个int没问题,所有其他值都给出了分段错误
  • 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 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; }

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. 如果我们使用范围解析运算符在类声明之外定义getter函数,则代码可以正常工作。

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. 据我所知,在C ++中,如果我们在类声明中定义一个函数,默认情况下它是内联的......所以问题可能与内联函数有关。

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... 进一步思考在内存中创建对象的方式,我认为QString可能不会保留固定的字节数,这可能是这种奇怪行为的原因并猜测是什么,虚拟更改解决了我的问题......

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; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM