Note that there are some explanatory texts on larger screens.

plurals
  1. POQObject double deletion
    text
    copied!<p>I am currently running Qt 4.7.4 on Mac OS X 10.6. I installed Qt using MacPorts.</p> <p>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:</p> <blockquote> <p>INFO : periodictable::ElementTest::testName() Signal: QObject(7fff5fbfd860) destroyed ((QObject*)7fff5fbfd860)</p> </blockquote> <p>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.</p> <pre><code>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&amp; name); QString name() const; Q_SIGNALS: void valueChanged(QString value); private: QString elementName; Q_DISABLE_COPY(Element); }; </code></pre> <p>I use the following command (via cmake):</p> <blockquote> <p>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</p> </blockquote> <p>I cannot recall experiencing this problem with Qt 4.6, and I am confused as to the premature destruction.</p> <p>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.</p> <p>-- Edit --</p> <p>Source code for test case:</p> <p>in .h file</p> <pre><code>#ifndef TEST_ELEMENT_H #define TEST_ELEMENT_H #include &lt;QtCore/QObject&gt; #include &lt;QtCore/QPointer&gt; namespace hashtable { class Element; // Forward declaration class ElementTest : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase(); void testName(); private: QString name; QPointer&lt;Element&gt; element; }; } #endif </code></pre> <p>in .cpp file</p> <pre><code>void ElementTest::initTestCase() { name = QString("Hydrogen"); mass = 1.008; QPointer&lt;Element&gt; element(new Element(this)); return; } void ElementTest::testName() { element-&gt;setProperty("name", name); QCOMPARE(element-&gt;property("name").toString(), name); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload