Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. develop my own signal scope with graphicsView / how to clear memory from deleted object in c++?

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 975 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M_Hu
    wrote on last edited by M_Hu
    #1

    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 
    

    }

    jsulmJ 1 Reply Last reply
    0
    • M M_Hu

      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 
      

      }

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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?

        M Offline
        M Offline
        M_Hu
        wrote on last edited by
        #3

        @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.

        jsulmJ 1 Reply Last reply
        0
        • M M_Hu

          @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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          2
          • jsulmJ jsulm

            @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.

            M Offline
            M Offline
            M_Hu
            wrote on last edited by
            #5

            @jsulm
            thank you. I did not care about that

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved