开发者

QObject double deletion

开发者 https://www.devze.com 2023-04-03 19:14 出处:网络
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 purpo

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


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);    
0

精彩评论

暂无评论...
验证码 换一张
取 消