欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Qt编译错误Error - RtlWerpReportException failed with status code :-1073741823

程序员文章站 2022-03-02 14:36:25
...

这是一个比较新手入门常见的问题,其实说白了就是内存泄漏,很具体的说大部分就是你对类里面的一个指针变量没有初始化,一下面的例子为例,m_tooltip未初始化为0导致后面的判断未申请内存导致内存泄漏

#ifndef MYBARVIEW_H
#define MYBARVIEW_H

#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QLegend>
#include <QtCharts/QBarCategoryAxis>
#include"charttip.h"
#include"common.h"
class QGraphicsScene;
QT_CHARTS_BEGIN_NAMESPACE
class QChart;
QT_CHARTS_END_NAMESPACE

class Callout;

QT_CHARTS_USE_NAMESPACE
class MyBarView: public QGraphicsView
{
    Q_OBJECT

public:
    MyBarView(BarViewParam *bvp, QWidget *parent = 0);
    void addBar(int setNum,QStringList CategoryNm,QVector<QVector<int> > setValList);

public:
    ChartTip*   m_tooltip; //以这个变量为例
    QChartView *m_chartView;
    QStringList  m_categories;
    QBarSeries *m_series;
    QChart  *m_myBarView;
    QVector<QBarSet*> m_barSetList;
    QBarCategoryAxis *m_typeAxis;
    QValueAxis* m_typeAxisY;
private:

public  slots:
    void sltTooltip(bool status, int index, QBarSet *barset);

};

#endif // MYBARVIEW_H
MyBarView::MyBarView(BarViewParam *bvp,QWidget *parent)
    : QGraphicsView(new QGraphicsScene, parent)
{
    //m_tooltip=0; 注释掉就会存在内存泄漏
    m_myBarView=new QChart();
    m_myBarView->setAnimationOptions(QChart::SeriesAnimations);
    m_series=new QBarSeries(m_myBarView);

    for(int i=0;i<bvp->m_setsNameList.size();i++)
    {
        QBarSet *set0= new QBarSet(bvp->m_setsNameList[i]);
        m_barSetList.append(set0);
        m_series->append(set0);
    }

//    QBarSet *set0 = new QBarSet("Samantha");
     //*set0 << 1 << 2 << 3 << 4 << 5 << 6;// Jane 6个月份的值
    m_series->setLabelsVisible(true);
    m_series->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd);

    connect(m_series, SIGNAL(hovered(bool, int, QBarSet*)), this, SLOT(sltTooltip(bool, int, QBarSet*)));
    m_myBarView->addSeries(m_series);
    m_myBarView->setTitle(bvp->m_title);


    QStringList categories;
    for(int i=0;i<bvp->m_categorieNameList.size();i++)
    {
        categories.append(bvp->m_categorieNameList[i]);
    }
    //categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";

    m_typeAxis = new QBarCategoryAxis();
    //m_typeAxis->append(categories);
    m_myBarView->createDefaultAxes();//创建默认的左侧的坐标轴(根据 QBarSet 设置的值)
    m_myBarView->setAxisX(m_typeAxis, m_series);//设置坐标轴

    m_typeAxisY = new QValueAxis;
    m_typeAxisY->setRange(0, 40);
    m_typeAxisY->setTickCount(10);     //设置多少格
    m_typeAxisY->setMinorTickCount(5); //设置每格小刻度线的数目

    m_myBarView->setAxisY(m_typeAxisY, m_series);

    m_myBarView->legend()->setVisible(true); //设置图例为显示状态
    m_myBarView->legend()->setAlignment(Qt::AlignBottom);//设置图例的显示位置在底部

    //m_myBarView->setAcceptTouchEvents(true);
    m_chartView=new QChartView(m_myBarView);
    m_chartView->setRenderHint(QPainter::Antialiasing);



}

void MyBarView::sltTooltip(bool status, int index, QBarSet *barset)
{
    if (m_tooltip == 0)// 由于注释掉了m_tooltip = 0,所以会执行下面的语句导致内存泄漏
        m_tooltip = new ChartTip(m_myBarView);

    if (status) {
        m_tooltip->setText("X: "+m_categories.at(index)+"\n"+QString("Y: %2 ").arg(barset->at(index)));
        QPointF point(index, barset->at(index));
        m_tooltip->setAnchor(point);
        m_tooltip->setZValue(11);
        m_tooltip->updateGeometry();
        m_tooltip->show();
    }
    else {
        m_tooltip->hide();
    }
}

 

相关标签: 内存泄漏