QObject双删除

发布于 2024-12-04 01:43:55 字数 2493 浏览 1 评论 0原文

我目前在 Mac OS X 10.6 上运行 Qt 4.7.4。我使用 MacPorts 安装了 Qt。

我一直在尝试使用测试驱动开发作为我的编码实践的一部分,并且我正在使用 QtTest 来实现此目的。我有一个从 QObject 派生的类,当我尝试测试代码时,我的测试在应该通过的时候失败了。我查看了 (test -vs) 的输出,发现以下错误:

信息:周期表::ElementTest::testName() 信号:QObject(7fff5fbfd860) 被破坏 ((QObject*)7fff5fbfd860)

在测试用例中,我观察到上述错误两次,夹在实际测试中。这表明子对象在使用前被销毁,并且在测试后似乎再次被删除。我使用过QPointer,并确认该对象在使用前变得无效。另一种方法是在每个测试用例中初始化变量,从而违背了单次初始化的目的,进而增加了代码膨胀。

class Element : public QObject
{
   Q_OBJECT
   Q_PROPERTY(QString name READ name WRITE setName NOTIFY valueChanged)
public:
   Element(QObject* parent = 0) : QObject(parent) {}
   void setName(const QString& name);
   QString name() const;
Q_SIGNALS:
   void valueChanged(QString value);
private:
   QString elementName;
   Q_DISABLE_COPY(Element);
};

我使用以下命令(通过 cmake):

g++ -D_FORTIFY_SOURCE=2 -D_GLIBCXX_FULLY_DYNAMIC_STRING -D_FORTIFY_SOURCE=2 -DQT_TEST_LIB -DQT_CORE_LIB -DQT_DEBUG -Wformat-security -Wmissing-format-attribute -Wformat=2 -Wctor-dtor-privacy -Wabi -Woverloaded-virtual -Wsign-promo -Wformat-nonliteral -Wdisabled-optimization -Wformat-y2k -Winit-self -Winvalid-pch -Wunsafe-loop-optimizations -Wmissing-format-attribute -Wmissing-include-dirs -Wstrict-aliasing =3 -Wswitch-enum -Wvariadic-macros -Wvolatile-register-var -std=gnu++0x -fmessage-length=0 -ftree-vectorize --param max-unroll-times=4 -pipe -fabi-version=4 -g -I/opt/local/include/QtCore -fPIC - fstack-保护器-fPIC -fstack-保护器-Wstack-保护器

我不记得在 Qt 4.6 中遇到过这个问题,而且我对过早的破坏感到困惑。

我想这不是 Qt 中的错误,但我很好奇是否有其他人遇到过这样的问题并找到了解决方案。我喜欢Qt,但这个问题不会仅限于单元测试。任何帮助都将不胜感激。

-- 编辑 --

测试用例的源代码:

在 .h 文件中

#ifndef  TEST_ELEMENT_H
#define  TEST_ELEMENT_H

#include    <QtCore/QObject>
#include    <QtCore/QPointer>

namespace hashtable
{

class Element;                                  // Forward declaration

class ElementTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void initTestCase();

    void testName();

private:
    QString name;
    QPointer<Element> element;
};

}
#endif

在 .cpp 文件中

void ElementTest::initTestCase()
{
    name = QString("Hydrogen");
    mass = 1.008;
    QPointer<Element> element(new Element(this));
    return;
}

void ElementTest::testName()
{

    element->setProperty("name", name);
    QCOMPARE(element->property("name").toString(), name);
}

I am currently running Qt 4.7.4 on Mac OS X 10.6. I installed Qt using MacPorts.

I have been trying to use test-driven development as a part of my coding practice, and I am using QtTest for this purpose. I have a class derived from QObject, and when I try to test the code, my test fails when it should pass. I looked at the output of (test -vs), and I observe the following error:

INFO : periodictable::ElementTest::testName() Signal: QObject(7fff5fbfd860) destroyed ((QObject*)7fff5fbfd860)

In a test case, I observe the above error twice, sandwiching the actual test. This indicates that the child object is destroyed before usage and seemingly deleted again after the test. I have used QPointer and confirmed that the object becomes invalid before usage. The alternative is to initialize the variables within each test case, thus defeating the purpose of a single-shot initialization and in turn, increasing code bloat.

class Element : public QObject
{
   Q_OBJECT
   Q_PROPERTY(QString name READ name WRITE setName NOTIFY valueChanged)
public:
   Element(QObject* parent = 0) : QObject(parent) {}
   void setName(const QString& name);
   QString name() const;
Q_SIGNALS:
   void valueChanged(QString value);
private:
   QString elementName;
   Q_DISABLE_COPY(Element);
};

I use the following command (via cmake):

g++ -D_FORTIFY_SOURCE=2 -D_GLIBCXX_FULLY_DYNAMIC_STRING -D_FORTIFY_SOURCE=2 -DQT_TEST_LIB -DQT_CORE_LIB -DQT_DEBUG -Wformat-security -Wmissing-format-attribute -Wformat=2 -Wctor-dtor-privacy -Wabi -Woverloaded-virtual -Wsign-promo -Wformat-nonliteral -Wdisabled-optimization -Wformat-y2k -Winit-self -Winvalid-pch -Wunsafe-loop-optimizations -Wmissing-format-attribute -Wmissing-include-dirs -Wstrict-aliasing=3 -Wswitch-enum -Wvariadic-macros -Wvolatile-register-var -std=gnu++0x -fmessage-length=0 -ftree-vectorize --param max-unroll-times=4 -pipe -fabi-version=4 -g -I/opt/local/include/QtCore -fPIC -fstack-protector -fPIC -fstack-protector -Wstack-protector

I cannot recall experiencing this problem with Qt 4.6, and I am confused as to the premature destruction.

I would like to think that this is not a bug within Qt, but I am curious if anyone else had encountered such a problem and found a solution. I like Qt, but this problem will not be limited to the unit tests. Any help will be certainly appreciated.

-- Edit --

Source code for test case:

in .h file

#ifndef  TEST_ELEMENT_H
#define  TEST_ELEMENT_H

#include    <QtCore/QObject>
#include    <QtCore/QPointer>

namespace hashtable
{

class Element;                                  // Forward declaration

class ElementTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void initTestCase();

    void testName();

private:
    QString name;
    QPointer<Element> element;
};

}
#endif

in .cpp file

void ElementTest::initTestCase()
{
    name = QString("Hydrogen");
    mass = 1.008;
    QPointer<Element> element(new Element(this));
    return;
}

void ElementTest::testName()
{

    element->setProperty("name", name);
    QCOMPARE(element->property("name").toString(), name);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ左 2024-12-11 01:43:55

ElementTest::initTestCase() 中的这一行:

QPointer<Element> element(new Element(this));

创建一个名为 element 的局部变量,该变量与成员 ElementTest::element 无关。当 ElementTest::initTestCase() 返回时,本地变量会被销毁。

尝试将该行更改为:

element = new Element(this);    

This line in ElementTest::initTestCase():

QPointer<Element> element(new Element(this));

is creating a local variable named element that has nothing to do with the member ElementTest::element. The local vairable is getting destroyed when ElementTest::initTestCase() returns.

Try changing the line to:

element = new Element(this);    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文