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. Memoy leak using QtCharts

Memoy leak using QtCharts

Scheduled Pinned Locked Moved Unsolved General and Desktop
30 Posts 10 Posters 5.9k 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.
  • A Offline
    A Offline
    aawawa
    wrote on last edited by
    #1

    Hello everyone,

    I hope you are haveing a nice day,

    I am currently trying to plot indefinitely a graph on a chartview.

    I have memory leak that I can't find the origine.

    Here is a quiet simple code to illustrate this fact :

    void MyChartView::Draw(void)
    {
    	int Length = 10000;
    	double Frequency = 0.1;
    	chart()->removeAllSeries(); //here I remove the series
    	QLineSeries *SerieToAdd = new QLineSeries;
    	for (int i = 0; i < Length; i++)
    	{
    		SerieToAdd->append(i, sin(static_cast<double>(i) * 3.14*2*0.1)); 
    	}
    	chart()->addSeries(SerieToAdd); //here I add the serie
    }
    

    for information, in this example, MyChartView is a basic class, nothing really creazy:

    class MyChartView:public QChartView
    {
    public:
    	MyChartView();
    	~MyChartView();
    	void Draw(void);
    };
    

    If I launch 10000 times the function Draw like below, I will see a memory leak.

    0_1540814669924_Chartview.PNG

    Do you know what I am doing wrong?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kuzulis
      Qt Champions 2020
      wrote on last edited by
      #2

      Do you know what I am doing wrong?

      Just don't use QtCharts... Use, e.g Qwt.

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        How big is this leak ?
        Could you try to do
        MyChartView test; ( as in no new.ing)
        Currently the test is not deleted as you assign no parent.

        1 Reply Last reply
        3
        • A Offline
          A Offline
          aawawa
          wrote on last edited by
          #4

          @kuzulis : hahahhabut by the way. I want to understand here why it doesn't work in this case!

          @mrjj : something around 10ko for each time when Draw is used.

          There is a strange thing. If I code the code below :

          void MyChartView::Draw(void)
          {
          	int Length = 10000;
          	double Frequency = 0.1;
          
          	chart()->removeAllSeries(); //here I remove the series
          	QLineSeries *SerieToAdd = new QLineSeries;
          	for (int i = 0; i < Length; i++)
          	{
          		SerieToAdd->append(i, sin(static_cast<double>(i) * 3.14*2*0.1)); 
                                    //or whatever
          	}
          
          	//chart()->addSeries(SerieToAdd); //here I comment this line
          
          	delete SerieToAdd;
          
          }
          

          I don't have any memoy leak. But, if I wrote the same code but with the addSeries line, the leak appear... (here below)

          void MyChartView::Draw(void)
          {
          	int Length = 10000;
          	double Frequency = 0.1;
          
          	chart()->removeAllSeries(); //here I remove the series
          	QLineSeries *SerieToAdd = new QLineSeries;
          	for (int i = 0; i < Length; i++)
          	{
          		SerieToAdd->append(i, sin(static_cast<double>(i) * 3.14*2*0.1));
           //or whatever
          	}
          	chart()->addSeries(SerieToAdd); //here I add the serie!!!!!!!LEAK
          	delete SerieToAdd;
          }
          

          This is quiet strange because I am supposed to remove and to delete the series that I have added to the chart at each iteration with the command removeAllSeries() no? So normally the size of my object must be constant in time...

          Could you try to do MyChartView test; ( as in no new.ing) I am sorry i m not sure that I have understand what you asked me...

          Currently the test is not deleted as you assign no parent. Yes! I don't want to delete the test, I just want that its size remains constant in time...

          mrjjM 1 Reply Last reply
          0
          • A aawawa

            @kuzulis : hahahhabut by the way. I want to understand here why it doesn't work in this case!

            @mrjj : something around 10ko for each time when Draw is used.

            There is a strange thing. If I code the code below :

            void MyChartView::Draw(void)
            {
            	int Length = 10000;
            	double Frequency = 0.1;
            
            	chart()->removeAllSeries(); //here I remove the series
            	QLineSeries *SerieToAdd = new QLineSeries;
            	for (int i = 0; i < Length; i++)
            	{
            		SerieToAdd->append(i, sin(static_cast<double>(i) * 3.14*2*0.1)); 
                                      //or whatever
            	}
            
            	//chart()->addSeries(SerieToAdd); //here I comment this line
            
            	delete SerieToAdd;
            
            }
            

            I don't have any memoy leak. But, if I wrote the same code but with the addSeries line, the leak appear... (here below)

            void MyChartView::Draw(void)
            {
            	int Length = 10000;
            	double Frequency = 0.1;
            
            	chart()->removeAllSeries(); //here I remove the series
            	QLineSeries *SerieToAdd = new QLineSeries;
            	for (int i = 0; i < Length; i++)
            	{
            		SerieToAdd->append(i, sin(static_cast<double>(i) * 3.14*2*0.1));
             //or whatever
            	}
            	chart()->addSeries(SerieToAdd); //here I add the serie!!!!!!!LEAK
            	delete SerieToAdd;
            }
            

            This is quiet strange because I am supposed to remove and to delete the series that I have added to the chart at each iteration with the command removeAllSeries() no? So normally the size of my object must be constant in time...

            Could you try to do MyChartView test; ( as in no new.ing) I am sorry i m not sure that I have understand what you asked me...

            Currently the test is not deleted as you assign no parent. Yes! I don't want to delete the test, I just want that its size remains constant in time...

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi

            • Currently the test is not deleted as you assign no parent. Yes! I don't want to delete the test, I just want that its size remains constant in time...

            I understand. i was just wondering if the lost memory was due to some house keeping in ChartView
            that would only be freed on app exit./ Widget destruction.

            However, it does seem odd as removeAllSeries says it will delete it and
            addSeries says it takes ownership so all should be fine.

            Could you try rem out
            // SerieToAdd->append(..
            and see if mem use is affected. Then we know its directly related to that.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              A small, compilable testcase would be the best here so we can check it (and maybe at it to the bugreport later on) :)

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • K Offline
                K Offline
                kuzulis
                Qt Champions 2020
                wrote on last edited by kuzulis
                #7

                @aawawa said in Memoy leak using QtCharts:

                hahahhabut by the way. I want to understand here why it doesn't work in this case!

                I'm too tried to use QtCharts and too faced with a memory leaks and etc.. I spat on QtCharts and replaced it with Qwt. (Because I haven't time to understand why QtCharts does not work as expected.. This was in Qt 5.9, as I remember).

                kshegunovK 1 Reply Last reply
                0
                • K kuzulis

                  @aawawa said in Memoy leak using QtCharts:

                  hahahhabut by the way. I want to understand here why it doesn't work in this case!

                  I'm too tried to use QtCharts and too faced with a memory leaks and etc.. I spat on QtCharts and replaced it with Qwt. (Because I haven't time to understand why QtCharts does not work as expected.. This was in Qt 5.9, as I remember).

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @kuzulis said in Memoy leak using QtCharts:

                  I spat on QtCharts and replaced it with Qwt. (Because I haven't time to unterstand why QtCharts does not work as expected.. This was in Qt 5.9, as I remember).

                  It hasn't improved since then. I did the same as you but picked QCustomPlot instead.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    aawawa
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher : please find a micro project here : link text

                    @mrjj : if I comment as this :

                    //SerieToAdd->append(i, sin(static_cast<double>(i) * 3.14*2
                    

                    I have always the memory leak but it seems to be less important (around 6ko)

                    mrjjM 1 Reply Last reply
                    0
                    • A aawawa

                      @Christian-Ehrlicher : please find a micro project here : link text

                      @mrjj : if I comment as this :

                      //SerieToAdd->append(i, sin(static_cast<double>(i) * 3.14*2
                      

                      I have always the memory leak but it seems to be less important (around 6ko)

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @aawawa
                      Ok so it does seems related to the series.
                      If you want to dig into it, you can very handy browse code here
                      https://code.woboq.org/qt5/

                      Super with sample.

                      1 Reply Last reply
                      1
                      • A Offline
                        A Offline
                        aawawa
                        wrote on last edited by
                        #11

                        @mrjj I 'm going to take a look to your link. Is it a database or you want me to send my code there?

                        mrjjM 1 Reply Last reply
                        0
                        • A aawawa

                          @mrjj I 'm going to take a look to your link. Is it a database or you want me to send my code there?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @aawawa
                          Hi
                          Its a code browser. it makes it really easy to see what is being used. all call to functions are links and
                          its almost as good as the debugger. at least to see what is going on.
                          I was thinking you would go and look how it deletes the series or something like that.
                          The sample is for Christian-Ehrlicher as he might make it into a bug report if we can
                          reproduce it and find no good reason for not release the mem on RemoveAllSeries.

                          1 Reply Last reply
                          1
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            I can see some kind of 'leak' but need to do some more investigations.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            2
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Ok, no leak there. Just a lot of data. You're creating 100000 QLineSeries with 10000 each - this will need some memory and time. I would rather try to add the points to one QLineSeries.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              1
                              • A Offline
                                A Offline
                                aawawa
                                wrote on last edited by
                                #15

                                @Christian-Ehrlicher thank you... But it is supposed to be deleted 100000 times in my case no when I use RemoveAllSeries no?

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  aawawa
                                  wrote on last edited by
                                  #16

                                  @Christian-Ehrlicher would rather try to add the points to one QLineSeries this strategy works quiet well.
                                  By changing y program as this for the header :

                                  class MyChartView:public QChartView
                                  {
                                  public:
                                  	MyChartView();
                                  	~MyChartView();
                                  	void Draw(void);
                                  
                                  private:	
                                  	QLineSeries *mSeries;
                                  };
                                  

                                  and for the .cpp

                                  MyChartView::MyChartView()
                                  {
                                  
                                  	mSeries = new QLineSeries;
                                  	chart()->addSeries(mSeries);
                                  
                                  }
                                  
                                  
                                  MyChartView::~MyChartView()
                                  {
                                  }
                                  
                                  void MyChartView::Draw(void)
                                  {
                                  	int Length = 1000;
                                  	mSeries->clear();
                                  	for (int i = 0; i < Length; i++)
                                  	{
                                  		mSeries->append(QPoint (i, sin(2 * i*3.141516*0.1)));
                                  	}
                                  	//update();
                                  
                                  }
                                  
                                  

                                  It seems (I have to confirm this point later) to not have anymore memory leak

                                  1 Reply Last reply
                                  1
                                  • Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @aawawa said in Memoy leak using QtCharts:

                                    But it is supposed to be deleted 100000 times in my case no when I use RemoveAllSeries no?

                                    You're right. I think I found the culprit:

                                    qlineseries.cpp:112
                                    
                                    QLineSeries::QLineSeries(QObject *parent)
                                        : QXYSeries(*new QLineSeriesPrivate(this), parent)
                                    

                                    this is never deleted... :/

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    JonBJ 1 Reply Last reply
                                    3
                                    • Christian EhrlicherC Christian Ehrlicher

                                      @aawawa said in Memoy leak using QtCharts:

                                      But it is supposed to be deleted 100000 times in my case no when I use RemoveAllSeries no?

                                      You're right. I think I found the culprit:

                                      qlineseries.cpp:112
                                      
                                      QLineSeries::QLineSeries(QObject *parent)
                                          : QXYSeries(*new QLineSeriesPrivate(this), parent)
                                      

                                      this is never deleted... :/

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #18

                                      @Christian-Ehrlicher
                                      I think this is the second time I've seen that *new cause this. When will people decide that C++ is getting too clever for its boots? ;-)

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by SGaist
                                        #19

                                        Hi,

                                        @Christian-Ehrlicher said in Memoy leak using QtCharts:

                                        QLineSeriesPrivate

                                        It's QObject based, so when the QXYSeries object is deleted it should also get deleted.

                                        It's indeed QObject based but stored in a QScopedPointer so it will automatically get delete on object's destruction.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        1
                                        • A Offline
                                          A Offline
                                          aawawa
                                          wrote on last edited by
                                          #20

                                          @Christian-Ehrlicher Thank you. It's the first time I see *new in c++ what does it mean?

                                          mrjjM 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