I'm facing a memory leak problem using QPainter on a QPixmap. I need to set about 250000 points around earth, on a map (nav points). Each of them share the same icon but have a specific label. I add all these points to the same layer.
Here is my code :
void Gspv::addFixes() // waypoints layer
{
waypoints = new GeometryLayer("Waypoints", mapadapter);
mc->addLayer(waypoints);
//Icon
QPixmap icone(38,38);
icone.fill(Qt::transparent);
QPainter *paint = new QPainter(&icone);
paint->setRenderHint(QPainter::Antialiasing);
paint->setBrush(Qt::cyan);
paint->setPen(Qt::black);
static const QPoint triangle[3] = {
QPoint(15,0),
QPoint(3, 20),
QPoint(27,20)
};
paint->drawPolygon(triangle, 3);
delete paint;
//Check file
QFile file(QCoreApplication::applicationDirPath() + "/data/Waypoints.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::information(0, "erreur lecture fichier : " + file.fileName(), file.errorString());
return;
}
//Parsing file
QTextStream in(&file);
while(!in.atEnd()) {
QString line = in.readLine();
QStringList fields = line.split(",");
QString str = fields.at(1);
double latitude = str.toDouble();
str = fields.at(2);
double longitude = str.toDouble();
str = fields.at(0);
addCode(icone,str); // Prob here
//Add current point to layer
Point* pointCourant = new Point(longitude, latitude, icone, str);
pointCourant->setBaselevel(10);
pointCourant->setMaxsize(QSize(38, 38));
waypoints->addGeometry(pointCourant);
}
file.close();
}
//Add code to QPixmap
void Gspv::addCode(QPixmap &pm, QString &code)
{
QPainter pmp(&pm);
pmp.setPen(Qt::black);
pmp.setFont(QFont("ArialBold",9));
pmp.eraseRect(0,20,38,15);
pmp.drawText(0,32,code);
}
Everything works as expected, except that it causes severe memory leaks. Problem is when adding the code within the while loop. Whatever I do (addcode in the addfixes method or in a specific one as it is with addCode), I get a memory leak.
Even if the code is only reduced to :
void Gspv::addCode(QPixmap &pm, QString &code)
{
QPainter pmp(&pm);
// here it does nothing !
}
the memory leak is. And no matter whether the statement is static or dynamic, the result is the same.
Without the addition of the code, the memory usage is about 152 Mo, which is low enough. With the addition of this simple code, it bugs out of memory.
I have read many posts on the QPainter and the memory leak, but I can't handle it.
Will you have an help for that ?
Thanks in advance.