develop my own signal scope with graphicsView / how to clear memory from deleted object in c++?



  • I want to develop my own signal scope in C++. So I use qt for this purpose. I add a graphicsView and a push Button in ui. At connected method to push button I update the graphicsView(finally I'll pass this method to a thread). Each time that I press push button, Despite the deleting pointer the usage of memory is increase. How should I control this?

    I check memory usage in vs15 diagnostic tool.

    c++ header file:

    #pragma once

    #include <QtWidgets/QMainWindow>
    #include "ui_QtGuiApplication.h"
    #include <QGraphicsScene>
    #include <QGraphicsPathItem>
    class QtGuiApplication : public QMainWindow
    {
    Q_OBJECT

    public:
    QtGuiApplication(QWidget *parent = Q_NULLPTR);

    private:
    Ui::QtGuiApplicationClass ui;

    QGraphicsScene* scene = new QGraphicsScene();
    QPolygon pol;
    QGraphicsPathItem* pathItem = new QGraphicsPathItem();
    int index_ = 0;            // graph offset
    QPen* myPen = new QPen(Qt::red, 2);
    
    private slots:  
    void btn_Display_clicked();
    

    };
    c++ source file:

    #include "QtGuiApplication.h"
    

    #include <math.h> /* sin */

    #define pi 3.14159265

    QtGuiApplication::QtGuiApplication(QWidget *parent)
    : QMainWindow(parent)
    {
    ui.setupUi(this);
    ui.graphicsView->setScene(scene);
    ui.graphicsView->show();
    connect(ui.btn_Display, SIGNAL(clicked()), this, SLOT(btn_Display_clicked()));
    }

    void QtGuiApplication::btn_Display_clicked()
    {
    scene->removeItem(pathItem);
    QPoint pos;

    double x_amp_, y_amp_;
    int x_pix_, y_pix_;
    double phi_ = 0;
    double freq_ = 5;
    for (int i = 0; i<800; i++)
    {
        y_amp_ = 100 * sin(2 * pi*freq_*i / 811 + phi_) + 20 * index_;
        x_amp_ = i;
        x_pix_ = x_amp_;
        y_pix_ = y_amp_;
        pos.setX(x_pix_);
        pos.setY(y_pix_);
        pol.append(pos);
    }
    QPainterPath* myPath = new QPainterPath();
    (*myPath).addPolygon(pol);
    pathItem = scene->addPath(*myPath, *myPen);
    delete myPath;
    pol.clear();
    index_ = (index_ + 1) % 20; // just for sense of change in graph 
    

    }


  • Moderators

    @M_Hu said in develop my own signal scope with graphicsView / how to clear memory from deleted object in c++?:

    scene->removeItem(pathItem);

    add this as removeItem() does not delete the item as stated in the documentation:

    delete pathItem;
    
    (*myPath).addPolygon(pol);
    

    why not simply

    myPath->addPolygon(pol);
    

    ?
    There is really no need to allocate myPath on the heap:

    QPainterPath myPath;
    myPath.addPolygon(pol);
    pathItem = scene->addPath(myPath, *myPen);
    

    You use way too much pointers... For example: why is myPen a pointer?



  • @jsulm
    if pathItem deleted then I'll lose pointer so How can I remove previous drawn path?! I want to erase previous path and plot new path at this method.
    in future I'll pass this method on a thread. so I think as far as I can use pointer an don't create new object.


  • Moderators

    @M_Hu Don't you get a new one here:

    pathItem = scene->addPath(*myPath, *myPen);
    

    ?
    Read this: http://doc.qt.io/qt-5/qgraphicsscene.html#addPath
    "Creates and adds a path item to the scene, and returns the item pointer."
    So, each time you call addPath you create a NEW item, which YOU have to delete later when you remove it from the scene.

    "if pathItem deleted then I'll lose pointer" - you don't need that pointer as you delete the old item and then create new one as described above.



  • @jsulm
    thank you. I did not care about that


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.