Details
-
Bug
-
Resolution: Done
-
P1: Critical
-
4.7.0
-
None
-
32f8c01196f6ebc2bd18ee9f66929bdba61fea20
Description
The documentation says:
Note: The editor does not take ownership of the document unless it is the document's parent object. The parent object of the provided document remains the owner of the object.
If the current document is a child of the text editor, then it is deleted.
This does not work:
#include <QtGui> class MyTextDocument : public QTextDocument { public: MyTextDocument(QObject *parent) : QTextDocument(parent) {} ~MyTextDocument() { qDebug() << "delete:" << objectName(); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); QTextEdit edit; MyTextDocument *a = new MyTextDocument(&edit); a->setObjectName("a"); edit.setDocument(a); MyTextDocument *b = new MyTextDocument(&edit); b->setObjectName("b"); qDebug() << "should delete 'a'"; edit.setDocument(b); return 0; }
I suspect the problem is in QTextControl::setDocument:
if (d->doc->parent() == this) delete d->doc;
This only checks if the QTextControl is parent, not the actual QTextEdit.
Of course the QTextDocument objects will finally be deleted on destruction of the QTextEdit (due to standard parent/child relationship), but if you follow the documentation this kind of leaks memory during runtime.