Absurd Qt QUI memory leak



  • I have an easily replicable memory leak inside my program. Why it's happening makes absolutely no sense to me. I was able to track it down to its source by commenting out lines and testing until I found the problem. I know this isn't the idea way to debug such problems, but I am a noob at this. The absurd result i got is shown below.

    The following lines cause a memory leak:

    @
    ...
    // Determine Position
    QFontMetrics fontMetrics(font);
    int lineWidth = fontMetrics.width(aText);
    ...
    @

    Where as commenting out the second line prevents the leak as shown below:

    @
    ...
    // Determine Position
    QFontMetrics fontMetrics(font);
    //int lineWidth = fontMetrics.width(aText);
    ...
    @

    I am absolutely sure the above is correct. I have thoroughly tested it, and know the ins and outs of replicating this particular memory leak.

    My guesses thus far are as follows:

    1. The real problem is likely hidden via c++ optimizations being done to the code. Perhaps the problem is upstream, and commenting / um-commenting of this line decides what code is thrown away by the compiler.
    2. There is something really weird happening on the Qt side of things with regards to memory.

    Now I must apologize for not including more of the source code, the code is rather lengthy, and is part of a massive program in the 10,000s of lines. But The Context of the source code is shown below.

    @
    // Centered Text
    void DisplayShapeDrawer::drawCenteredText(
    QWidget * viewport,
    QString & aText,
    QColor & aColor,
    int aFontSize,
    int aCenterX,
    int aCenterY)
    {
    // Make Pen
    QPen pen(aColor);

    // Make Font
    QFont font("Arial", aFontSize);
    
    // Determine Position
    QFontMetrics fontMetrics(font);
    int lineWidth  = fontMetrics.width(aText);
    //int lineHeight = fontMetrics.height();
    
    /*int left   = aCenterX - (lineWidth  / 2);
    int bottom = aCenterY + (lineHeight / 2);
    QPoint pos(left, bottom);
    
    // Draw Text
    QPainter painter;
    painter.begin(viewport);
    painter.setPen(pen);
    painter.setFont(font);
    painter.drawText(pos, aText);
    painter.end();*/
    

    }

    void ReadingResult::drawTextWords(
    QWidget * viewport,
    QRect & dest,
    QRect & source,
    QColor & colorText,
    int wordIndexStart,
    int wordIndexEnd,
    int fontSize)
    {
    // For all words in line
    for (int wordIndex = wordIndexStart; wordIndex <= wordIndexEnd; wordIndex++)
    {
    // Get Word
    QString word = this->wordString(wordIndex);

        // Check For Overlap with area that is being drawn
        QRect wordRect = this->wordRect(wordIndex);
        bool isOverlapping = wordRect.intersects(source);
        if (isOverlapping)
        {
            // Draw Text
            QRect displayRect = Utilities::mapRect(source, dest, wordRect);
            int centerX  = displayRect.center().x();
            int centerY  = displayRect.center().y();
            DisplayShapeDrawer::drawCenteredText(viewport, word, colorText, fontSize, centerX, centerY);
            DisplayShapeDrawer::drawEmptyRect(viewport, displayRect, 5, QColor(0,255,0));
        }
    }
    

    }
    @

    Any help would be greatly appreciated.

    Thanks,
    Wargamer

    [edit: code tags added, koahnig]


  • Moderators

    Please check out the "forum guidelines for code wrappings":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.