Results 1 to 12 of 12

Thread: Memory Leak with QPrinter/QPainter

  1. #1
    Join Date
    Oct 2008
    Location
    Germany
    Posts
    29
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Memory Leak with QPrinter/QPainter

    Hi,

    if I print a lot of QImages using QPrinter (with preview in QPrintPreviewDialog) the memory usage of my program increases a lot and after finishing printing the memory still isn't released.

    Example:
    Qt Code:
    1. QPrinter *printer = new Printer();
    2. ...
    3. ...
    4. ...
    5. QPainter painter;
    6. painter.begin(printer);
    7. QImage image;
    8. image.load(...);
    9. painter.drawImage(...,image);
    10. painter.end();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Memory Leak with QPrinter/QPainter

    is the snippet of code you posted in a loop?
    Can you post the code "frame" of this code? (relevant before and after lines.)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Memory Leak with QPrinter/QPainter

    a) where do you delete the printer?
    b) what tool are you using to measure the memory usage? Because it is like that the memory is freed, but still reserved for the application.

  4. #4
    Join Date
    Oct 2008
    Location
    Germany
    Posts
    29
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Memory Leak with QPrinter/QPainter

    I'm using the activity monitor in Mac OSX (I think this is the equivalent to task manager in Windows)

    Here some more Code to understand the situation better:

    Constructor of my print object
    Qt Code:
    1. Drucken::Drucken() : QObject() {
    2. printer = new QPrinter(QPrinter::HighResolution);
    3. printer->setPageMargins(5.0,5.0,5.0,5.0,QPrinter::Millimeter);
    4. printer->setOrientation(orientation);
    5. if (outputType == 0) {
    6. printer->setOutputFormat(QPrinter::NativeFormat);
    7. } else {
    8. printer->setOutputFormat(QPrinter::PdfFormat);
    9. }
    10. dialog = new QPrintPreviewDialog(printer);
    11. dialog->setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
    12. dialog->setWindowTitle("TurnFix - Drucken");
    13. dialog->setWindowIcon(QIcon(":/icons/icon.png"));
    14. dialog->printer()->setPaperSize(paperSize);
    15. connect(dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
    16. }
    To copy to clipboard, switch view to plain text mode 

    print method
    Qt Code:
    1. void Drucken::print(QPrinter *prt) {
    2. curr_printer = prt;
    3. if (outputType == 2) curr_printer->setOutputFileName(outputFileName);
    4. pr = curr_printer->pageRect();
    5. painter.begin(curr_printer);
    6.  
    7. // This is painted on every page
    8. QSqlQuery layoutData;
    9. layoutData.prepare(...);
    10. layoutData.bindValue(0,...);
    11. layoutData.exec();
    12.  
    13. while (layoutData.next()) {
    14. if (!customImages.contains(layoutData.value(6).toString())) {
    15. QImage pm;
    16. pm.load(layoutData.value(6).toString());
    17. if (!pm.isNull()) {
    18. customImages.insert(layoutData.value(6).toString(),pm.scaled(mmToPixel(layoutData.value(4).toDouble()),mmToPixel(layoutData.value(5).toDouble()),Qt::KeepAspectRatio,Qt::SmoothTransformation));
    19. }
    20. pm.~QImage();
    21. }
    22. painter.drawImage(mmToPixel(layoutData.value(2).toDouble()),mmToPixel(layoutData.value(3).toDouble()),customImages.value(layoutData.value(6).toString()));
    23. }
    24. // End of code for every page
    25.  
    26. ...
    27.  
    28. painter.end();
    29. customImages.clear();
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Memory Leak with QPrinter/QPainter

    You didn't answer Lykurgs question, where you delete the printer pointer?
    You don't have to call pm.~QImage(); (line 20 second code block) since it will be called automatically at the end of the if() block.
    Also, what type of a container is 'customImages'?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Oct 2008
    Location
    Germany
    Posts
    29
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Memory Leak with QPrinter/QPainter

    I don't delete the printer pointer at the moment. I tried to delete it after painter.end() but that caused my program to crash.
    customImages is a QMap<QString,QImage>. I'm using that container to "store" the images because they are printed on every page.

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Memory Leak with QPrinter/QPainter

    don't delete the printer pointer at the moment.
    Well, that is one memory leak you have.
    Just parent your printer object, it will be destroyed when its parent is destroyed.
    See if this fixes the problem, if not, then make sure you are deleting or parenting all heap allocated QOjects.
    Objects that you allocate on the heap and are not QObjects you have to delete your self.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Memory Leak with QPrinter/QPainter

    Quote Originally Posted by cevou View Post
    I don't delete the printer pointer at the moment.
    ... and then you wonder about memory leaks
    parent it as high_flyer had suggested or simple delete them in the destructor of your Drucken class.

  9. #9
    Join Date
    Oct 2008
    Location
    Germany
    Posts
    29
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Memory Leak with QPrinter/QPainter

    Ok, it releases the memory now after printing or closing the QPrintPreviewDialog.
    However, while printing the memory usage still increases a lot (from 32MB to something like 600-700MB). Is that because of the pictures?

  10. #10
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Memory Leak with QPrinter/QPainter

    I'm not sure how many pictures you have, but this
    Qt Code:
    1. customImages.insert(...);
    To copy to clipboard, switch view to plain text mode 
    while you're printing will get pretty big pretty soon :-)
    700 images of 1MB = 700MB

    Why do you keep a list of images and clear it at the end?
    Isn't there another way without keeping all those pictures in memory?

  11. #11
    Join Date
    Oct 2008
    Location
    Germany
    Posts
    29
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Memory Leak with QPrinter/QPainter

    Usually there are only up to 5 pictures. These pictures are on every page and there can be over 100 pages. That's why I thought putting the pictures in a QMap would save memory.

  12. #12
    Join Date
    Oct 2008
    Location
    Germany
    Posts
    29
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Memory Leak with QPrinter/QPainter

    I tried using QPixmap instead of QImage now and it seems that it doesn't use so much memory anymore.

Similar Threads

  1. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 11:13
  2. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  3. memory leak
    By mattia in forum Newbie
    Replies: 18
    Last Post: 16th January 2008, 10:22
  4. Memory Leak in Qt
    By Krish_ng in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2007, 08:02
  5. Memory leak
    By zlatko in forum Qt Programming
    Replies: 8
    Last Post: 28th March 2006, 19:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.