PDA

View Full Version : Free memory - QPixmap::grabWindow?



hakermania
18th April 2011, 15:21
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

while(1){
sleep(3000);
QPixmap *a = new QPixmap;
*a = QPixmap::grabWindow(QApplication::desktop()->winId());
QImage *img = new QImage;
*img = a->toImage();
QRgb b = img->pixel(796,554);
QColor *c = new QColor;
c->setRgb(b);
qDebug() << c->name();
delete a;
delete c;
if(c->name() == "#968d82"){
system("canberra-gtk-play -f ~/Documents/Alert.wav");
}
Every time it goes through the loop the program takes almost 4MB of memory usage :rolleyes::confused::rolleyes:
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?

high_flyer
18th April 2011, 15:44
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.

stampede
18th April 2011, 15:51
delete c;
if(c->name() == "#968d82"){
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.

hakermania
18th April 2011, 16:10
Thx, deleting the img did the trick ;)