Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-14049

QTextEdit::setDocument does not delete current document as stated in the docs

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P1: Critical
    • 4.7.2
    • 4.7.0
    • GUI: Text handling
    • 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.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            esabraha Eskil Abrahamsen Blomfeldt
            njeisecke Nils Jeisecke
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes