title: Qt: Memory leaks in QGraphicsScene::addText?
body: <p>I'm trying to make a Rule for a Timeline like this:</p>
  
  <p><img src="https://i.stack.imgur.com/XENdL.jpg" alt="Timeline"></p>
  
  <p>I have a QGraphicsView where I put a QGraphicsScene where I add QGraphicsItems like lines and some labels.</p>
  
  <p>I add the elements to the Scene only when the zoom changes (and not when a paintEvent raises).</p>
  
  <p>For adding time labels, I use:</p>
  
  <pre><code>    QString label = "00:14"; // For example
      int posX = ... // Here I calculate the position of the label
      scene-&gt;addText(label,QFont("Arial",8))-&gt;setPos(posX,-1);
  </code></pre>
  
  <p>When I have to re-draw the Rule, I make:</p>
  
  <pre><code>    qDeleteAll(scene-&gt;items());
  </code></pre>
  
  <p>at the beggining and then add labels and lines again.</p>
  
  <p>I realized that I have bad performance. My scene has something like 8k elements (between lines and labels) so I used Valgrind to check for problems.</p>
  
  <p>It shows that 'possibly' I have memory leaks in the lines where I add text to the scene. I have some messages that look like:</p>
  
  <pre><code>    2,165,760 bytes in 470 blocks are possibly lost in loss record 9,922 of 9,923
        in TimelineWidget::drawRule() in Timeline/timelinewidget.cpp:166
        1: realloc in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
        2: /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.3
        3: /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.3
        4: QTextDocument::setPlainText(QString const&amp;amp;) in /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.3
        5: /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.3
        6: QGraphicsTextItem::QGraphicsTextItem(QString const&amp;amp;, QGraphicsItem*, QGraphicsScene*) in /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.3
        7: QGraphicsScene::addText(QString const&amp;amp;, QFont const&amp;amp;) in /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.3
        8: TimelineWidget::drawRule() 
  </code></pre>
  
  <p>This only happen with addText calls, not with addLine ones.</p>
  
  <p>I'm using <strong>Qt 4.8</strong> and my questions are:</p>
  
  <ul>
  <li><p>Are those memory leaks real?</p></li>
  <li><p>Am I doing something wrong?</p></li>
  <li><p>Is there another/a better way to implement my Rule?</p></li>
  </ul>
  
  <p>Thanks in advance!</p>