Results 1 to 4 of 4

Thread: Free memory - QPixmap::grabWindow?

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Free memory - QPixmap::grabWindow?

    I use this code in order to capture the color of a pixel, compare it with a HTML value and do an action if the comparisono is successful
    Qt Code:
    1. while(1){
    2. sleep(3000);
    3. QPixmap *a = new QPixmap;
    4. *a = QPixmap::grabWindow(QApplication::desktop()->winId());
    5. QImage *img = new QImage;
    6. *img = a->toImage();
    7. QRgb b = img->pixel(796,554);
    8. QColor *c = new QColor;
    9. c->setRgb(b);
    10. qDebug() << c->name();
    11. delete a;
    12. delete c;
    13. if(c->name() == "#968d82"){
    14. system("canberra-gtk-play -f ~/Documents/Alert.wav");
    15. }
    To copy to clipboard, switch view to plain text mode 
    Every time it goes through the loop the program takes almost 4MB of memory usage
    It's probaly because it grubs the whole desktop picture every time it runs...
    I tried to free some by deleting 'a' and 'c' but it didn't work. Any suggestion?
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  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: Free memory - QPixmap::grabWindow?

    Why do you use heap allocation in this case?
    You are using all variables only locally - so get rid of the heap allocation and use stack variables.
    See if this helps.
    ==========================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
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Free memory - QPixmap::grabWindow?

    Qt Code:
    1. delete c;
    2. if(c->name() == "#968d82"){
    To copy to clipboard, switch view to plain text mode 
    This is not good, you delete an object and then reference it, you were very lucky it didn't crash. Memory is leaking, because you don't have delete img; anywhere.
    Anyway, high_flyer is right, there is no need for heap allocation.

  4. The following user says thank you to stampede for this useful post:

    hakermania (18th April 2011)

  5. #4
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Free memory - QPixmap::grabWindow?

    Thx, deleting the img did the trick
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

Similar Threads

  1. Replies: 1
    Last Post: 23rd February 2012, 11:13
  2. QPixmap::grabWindow does not work on some machines!
    By dima72 in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2010, 07:02
  3. QPixmap::grabWindow() problem
    By mtroscheck in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2009, 00:15
  4. Replies: 1
    Last Post: 21st August 2008, 07:44
  5. QPixmap::grabWindow too slow
    By niko in forum Newbie
    Replies: 2
    Last Post: 31st August 2006, 06:40

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.