2014-04-02 2 views
2

코드에서 geap 손상 감지가 발생했습니다. 파괴 된 후에 오류가 발생합니다. 오류는 QwtLegend 및 QwtPlotCurve 포인터와 연결됩니다. 나는 auto_ptr을 사용하여 memery가 정확하게 할당 해제되었다는 것을 100 % 확신하지만 오류가 발생했다고 생각했습니다. 나는 이것이 QwtPlot에이 포인터를 전달하는 액션과 연결되어 있다고 생각한다. 누구나 올바르게 구현되어야하는 방법을 설명 할 수 있습니까? SSCCE 코드 아래 :QwtPlot - 힙 손상 감지

#include "plot.h" 

Plot::Plot(QWidget *parent) : 
    QwtPlot(parent) 
{ 
    setUpPlot(); 
    setUpCurves(); 
} 

    void Plot::setUpPlot() 
    { 
     legend = std::auto_ptr<QwtLegend>(new QwtLegend); 
     legend->setFrameStyle(QFrame::Box|QFrame::Sunken); 
     this->insertLegend(legend.get(), QwtPlot::BottomLegend); 
    } 

    void Plot::setUpCurves() 
    { 
     aXCurve = new QwtPlotCurve("Acceleration in X axis"); 
     aXCurve->attach(this); 
     replot(); 
    } 

Plot::~Plot() 
{ 
    aXCurve->detach(); 
    delete aXCurve; 
    aXCurve = NULL; 
} 

#ifndef PLOT_H 
#define PLOT_H 

#include <qwt_plot.h> 
#include <qwt_legend.h> 
#include <qwt_plot_curve.h> 

class Plot : public QwtPlot 
{ 
    Q_OBJECT 
public: 
    explicit Plot(QWidget *parent = 0); 
    ~Plot(); 

private: 
    void setUpPlot(); 
    void setUpCurves(); 

    std::auto_ptr<QwtLegend> legend; 
    QwtPlotCurve *aXCurve; 
}; 

#endif // PLOT_H 
+0

여기에 버그의 원인이 아니지만'std :: auto_ptr'을 사용하지 마십시오. 근본적으로 문제가 발생하며 단순한 오류의 원인 (예 : 할당/복사)을 수행 할 수 있습니다. 'std :: unique_ptr' 또는'QScopedPointer'가 필요합니다. –

답변

3

나는 같은 객체의 이중 삭제 (QwtLegend)이 코드에서가 발생 것으로 의심 : 인해 Plot 클래스 auto_ptr은을 사용하여

  • Qwt가 this->insertLegend(legend.get(), QwtPlot::BottomLegend); 호출로 플롯에 지정된 범례 포인터를 삭제한다고 의심합니다.

    QwtPlot::~QwtPlot() 
    { 
        [..] 
        delete d_data; // <- deletes the private data 
    } 
    

    를 그리고 개인 데이터를 참조 전설 삭제 QPointer을 사용합니다 : 그냥 QwtPlot 소스를 찾고이 분명하게 그래서

    class QwtPlot::PrivateData 
    { 
    public: 
        [..] 
        QPointer<QwtAbstractLegend> legend; // <-- will delete the legend 
    }; 
    

을, 당신이 명시 적으로 삭제할 필요가 없습니다 결론 legend, QwtPlot이 소유권을 갖는다는 사실에 의존합니다.