2017-10-26 25 views
0

QPixmap에서 QPainter를 사용하여 메모리 누수 문제가 있습니다. 지구상에서 약 250000 포인트를지도 (항법 점)에 설정해야합니다. 각각은 동일한 아이콘을 공유하지만 특정 레이블이 있습니다. 이 모든 점들을 같은 레이어에 추가합니다.Qpainter 메모리 누수 피하기

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); 
} 

모두가 심각한 메모리 누수가 발생하는 것을 제외하고 예상대로 작동합니다

여기 내 코드입니다. while 루프 내에 코드를 추가 할 때 문제가 발생합니다. 내가 무엇을 하든지 (addcode 메소드에있는 addcode 또는 addCode와 같은 특정 코드에있는 addcode), 나는 메모리 누수가 발생한다.

코드 만 감소된다하더라도 :

void Gspv::addCode(QPixmap &pm, QString &code) 
{ 
    QPainter pmp(&pm); 
    // here it does nothing ! 
} 

메모리 누수이다. 그리고 그 문장이 정적인지 동적인지 상관없이, 결과는 같습니다.

코드를 추가하지 않으면 메모리 사용량이 약 152Mo로 충분합니다. 이 간단한 코드를 추가하면 메모리 부족으로 버그가 발생합니다.

QPainter 및 메모리 누수에 대한 많은 게시물을 읽었지만 처리 할 수 ​​없습니다.

도움이 필요하십니까?

미리 감사드립니다.

+0

QPixmap 대신 QImage를 사용하십시오. – eyllanesc

+0

보기 : https://stackoverflow.com/questions/10307860/what-is-the-difference-between-qimage-and-qpixmap – eyllanesc

+0

화가가 누수의 근원이 확실합니까? 제작자가 가장 최근의 코드를 작성하고 실행 했습니까? 그것은 종종 붙어 있습니다 ... – dtech

답변

0

@AlexanderVX 의견에 따라 코드가 변경되었습니다. 다음은 새로운 구조입니다. 이에서

addFixes 방법이 될

void Gspv::addFixes() // waypoints layer 
{ 

... 
    addCode(icone,str); // Prob here 
... 
} 

은 :

void Gspv::addFixes() // waypoints layer 
{ 
... 
    //Add code within icon 
    paint = new QPainter(&icone); 
    QGraphicsItem *item = new GenIcone("wayPoint", str, paint); 
    delete paint; 
... 
} 

AddCode 방법은 새로운 클래스, GenIcon로 대체되었습니다.

#include "genicone.h" 
#include <QtWidgets> 
#include <QWidget> 
#include <QStyleOptionGraphicsItem> 

GenIcone::GenIcone(const QString icone, QString code, QPainter *painter) 
{ 
    this->m_icone = icone; 
    this->m_code = code; 

    setAcceptHoverEvents(true); 
    QStyleOptionGraphicsItem opt; 
    QWidget w; 

    paint(painter,&opt,&w); 
} 


void GenIcone::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) 
{ 
    Q_UNUSED(widget); 
    Q_UNUSED(item); 

    //Draw code within icon 

    if (m_icone== "wayPoint") { 
     painter->setFont(QFont("ArialBold",10)); 
     painter->eraseRect(0,20,50,18); 
     painter->drawText(1,32,m_code); 
     painter->end(); 
    } 
} 

QRectF GenIcone::boundingRect() const 
{ 
    ... 
} 

QPainterPath GenIcone::shape() const 
{ 
    ... 
} 

그래서, 작동 .H

#ifndef GENICONE_H 
#define GENICONE_H 

//#include <QPixmap> 
#include <QGraphicsItem> 
#include <QPainter> 

class GenIcone : public QGraphicsItem 
{ 
public: 
    GenIcone(const QString icone, QString code, QPainter *painter); 
    QRectF boundingRect() const Q_DECL_OVERRIDE; 
    QPainterPath shape() const Q_DECL_OVERRIDE; 
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) Q_DECL_OVERRIDE; 

    QPainter m_painter; 

private : 
    QString m_icone; 
    QString m_code; 
}; 

#endif // GENICONE_H 

통화 당

은,하지만, 난 여전히 페인트 과부하에도 불구하고 메모리 누수를 얻을. 코드 개선을 위해 무엇을 할 수 있습니까?

감사합니다.