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. Memory leak?
Qt 6.11 is out! See what's new in the release blog

Memory leak?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.4k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    Creatorczyk
    wrote on last edited by
    #1

    Hi,

    I implement class like below, but I am not sure if there is a memory leak.

    point.h

    class Point : public QwtPlotMarker
    {
    public:
        explicit Point(const QColor &color);
        void setPosition(const double x, const double y, const bool status);
    
    private:
        QwtSymbol *symbol;
        QColor color;
    };
    

    point.cpp

    Point::Point(const QColor &color) :
        color(color)
    {
        symbol = new QwtSymbol();
    }
    
    void Point::setPosition(const double x, const double y, const bool status)
    {
        if(status){
            symbol = new QwtSymbol(QwtSymbol::XCross);
        }else{
            symbol = new QwtSymbol(QwtSymbol::Ellipse);
        }
        setSymbol(symbol);
        setValue(x,y);
    }
    

    Is the code ok? Function setPosition () is called multiple times

    J.HilkJ KroMignonK 2 Replies Last reply
    0
    • C Creatorczyk

      Hi,

      I implement class like below, but I am not sure if there is a memory leak.

      point.h

      class Point : public QwtPlotMarker
      {
      public:
          explicit Point(const QColor &color);
          void setPosition(const double x, const double y, const bool status);
      
      private:
          QwtSymbol *symbol;
          QColor color;
      };
      

      point.cpp

      Point::Point(const QColor &color) :
          color(color)
      {
          symbol = new QwtSymbol();
      }
      
      void Point::setPosition(const double x, const double y, const bool status)
      {
          if(status){
              symbol = new QwtSymbol(QwtSymbol::XCross);
          }else{
              symbol = new QwtSymbol(QwtSymbol::Ellipse);
          }
          setSymbol(symbol);
          setValue(x,y);
      }
      

      Is the code ok? Function setPosition () is called multiple times

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #5

      @Creatorczyk said in Memory leak?:

      Is the code ok? Function setPosition () is called multiple times

      No, this code is not okay, you newer release the created objects!
      You have 3 ways to solve this:

      • add free to destroy old instances (and don't forget the destructor!!)
      • use smart pointers to not have to handle it yourself (cf. QSharedPointer())
      • no use pointer at all!
      class Point : public QwtPlotMarker
      {
      public:
          explicit Point(const QColor &color): color(color)  {}
          void setPosition(const double x, const double y, const bool status)
          {
              symbol.setStyle (status ? QwtSymbol::XCross : QwtSymbol::Ellipse);
              ...
          }
      
      private:
          QwtSymbol symbol;
          QColor color;
      };
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      0
      • C Creatorczyk

        Hi,

        I implement class like below, but I am not sure if there is a memory leak.

        point.h

        class Point : public QwtPlotMarker
        {
        public:
            explicit Point(const QColor &color);
            void setPosition(const double x, const double y, const bool status);
        
        private:
            QwtSymbol *symbol;
            QColor color;
        };
        

        point.cpp

        Point::Point(const QColor &color) :
            color(color)
        {
            symbol = new QwtSymbol();
        }
        
        void Point::setPosition(const double x, const double y, const bool status)
        {
            if(status){
                symbol = new QwtSymbol(QwtSymbol::XCross);
            }else{
                symbol = new QwtSymbol(QwtSymbol::Ellipse);
            }
            setSymbol(symbol);
            setValue(x,y);
        }
        

        Is the code ok? Function setPosition () is called multiple times

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #2

        @Creatorczyk said in Memory leak?:

        Is the code ok?

        no, its not. Its indeed leaking.

        private:
            QwtSymbol *symbol = nullptr;
        
        void Point::setPosition(const double x, const double y, const bool status)
        {
            if(symbol)
                delete symbol;
            if(status){
                symbol = new QwtSymbol(QwtSymbol::XCross);
            }else{
                symbol = new QwtSymbol(QwtSymbol::Ellipse);
            }
            setSymbol(symbol);
            setValue(x,y);
        }
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        C 1 Reply Last reply
        3
        • J.HilkJ J.Hilk

          @Creatorczyk said in Memory leak?:

          Is the code ok?

          no, its not. Its indeed leaking.

          private:
              QwtSymbol *symbol = nullptr;
          
          void Point::setPosition(const double x, const double y, const bool status)
          {
              if(symbol)
                  delete symbol;
              if(status){
                  symbol = new QwtSymbol(QwtSymbol::XCross);
              }else{
                  symbol = new QwtSymbol(QwtSymbol::Ellipse);
              }
              setSymbol(symbol);
              setValue(x,y);
          }
          
          C Offline
          C Offline
          Creatorczyk
          wrote on last edited by
          #3

          @J-Hilk The application crashes with your method

          J.HilkJ 1 Reply Last reply
          0
          • C Creatorczyk

            @J-Hilk The application crashes with your method

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #4

            @Creatorczyk where? where does the debugger break? Whats the stack trace like ?


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            1
            • C Creatorczyk

              Hi,

              I implement class like below, but I am not sure if there is a memory leak.

              point.h

              class Point : public QwtPlotMarker
              {
              public:
                  explicit Point(const QColor &color);
                  void setPosition(const double x, const double y, const bool status);
              
              private:
                  QwtSymbol *symbol;
                  QColor color;
              };
              

              point.cpp

              Point::Point(const QColor &color) :
                  color(color)
              {
                  symbol = new QwtSymbol();
              }
              
              void Point::setPosition(const double x, const double y, const bool status)
              {
                  if(status){
                      symbol = new QwtSymbol(QwtSymbol::XCross);
                  }else{
                      symbol = new QwtSymbol(QwtSymbol::Ellipse);
                  }
                  setSymbol(symbol);
                  setValue(x,y);
              }
              

              Is the code ok? Function setPosition () is called multiple times

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #5

              @Creatorczyk said in Memory leak?:

              Is the code ok? Function setPosition () is called multiple times

              No, this code is not okay, you newer release the created objects!
              You have 3 ways to solve this:

              • add free to destroy old instances (and don't forget the destructor!!)
              • use smart pointers to not have to handle it yourself (cf. QSharedPointer())
              • no use pointer at all!
              class Point : public QwtPlotMarker
              {
              public:
                  explicit Point(const QColor &color): color(color)  {}
                  void setPosition(const double x, const double y, const bool status)
                  {
                      symbol.setStyle (status ? QwtSymbol::XCross : QwtSymbol::Ellipse);
                      ...
                  }
              
              private:
                  QwtSymbol symbol;
                  QColor color;
              };
              

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mpergand
                wrote on last edited by mpergand
                #6

                Looking at QwtPlotMarker:: setSymbol:

                void QwtPlotMarker::setSymbol( const QwtSymbol* symbol )
                 {
                     if ( symbol != m_data->symbol )
                     {
                         delete m_data->symbol;
                         m_data->symbol = symbol;
                  
                         if ( symbol )
                             setLegendIconSize( symbol->boundingRect().size() );
                  
                         legendChanged();
                         itemChanged();
                     }
                 }
                
                

                explain the crash.

                ~PrivateData()
                     {
                         delete symbol;
                     }
                

                QwtPlotMarker delete symbol in the destructor.

                Conclusion: QwtPlotMarker do the right think,

                private:
                    QwtSymbol *symbol;
                

                this variable of class Point must be seen as a 'weak pointer' that is, this object is managed by another object (here QwtPlotMarker)

                J.HilkJ 1 Reply Last reply
                1
                • M mpergand

                  Looking at QwtPlotMarker:: setSymbol:

                  void QwtPlotMarker::setSymbol( const QwtSymbol* symbol )
                   {
                       if ( symbol != m_data->symbol )
                       {
                           delete m_data->symbol;
                           m_data->symbol = symbol;
                    
                           if ( symbol )
                               setLegendIconSize( symbol->boundingRect().size() );
                    
                           legendChanged();
                           itemChanged();
                       }
                   }
                  
                  

                  explain the crash.

                  ~PrivateData()
                       {
                           delete symbol;
                       }
                  

                  QwtPlotMarker delete symbol in the destructor.

                  Conclusion: QwtPlotMarker do the right think,

                  private:
                      QwtSymbol *symbol;
                  

                  this variable of class Point must be seen as a 'weak pointer' that is, this object is managed by another object (here QwtPlotMarker)

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #7

                  @mpergand oh, that explains a lot, deleting non 0 pointers twice is UB. the QwtPlotMaker does manage that memory.

                  Conclusion: QwtPlotMarker do the right think,

                  I would disagree that is not a good api design.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  M 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @mpergand oh, that explains a lot, deleting non 0 pointers twice is UB. the QwtPlotMaker does manage that memory.

                    Conclusion: QwtPlotMarker do the right think,

                    I would disagree that is not a good api design.

                    M Offline
                    M Offline
                    mpergand
                    wrote on last edited by
                    #8

                    @J-Hilk said in Memory leak?:

                    I would disagree that is not a good api design.

                    I fully agree with you.

                    I know nothing about QwtPlotMarker , but it must clearly be explained in the doc that setSymbol take ownership of symbol.
                    Yeah, it's bad design in C++.

                    KroMignonK 1 Reply Last reply
                    0
                    • M mpergand

                      @J-Hilk said in Memory leak?:

                      I would disagree that is not a good api design.

                      I fully agree with you.

                      I know nothing about QwtPlotMarker , but it must clearly be explained in the doc that setSymbol take ownership of symbol.
                      Yeah, it's bad design in C++.

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by
                      #9

                      @mpergand said in Memory leak?:

                      I know nothing about QwtPlotMarker , but it must clearly be explained in the doc that setSymbol take ownership of symbol.
                      Yeah, it's bad design in C++.

                      I never used Qwt, but for me, it look like you don't use it as it was designed...
                      Why do you have a QwtSymbol pointer in your Point class?
                      As QwtPlotMarker will take ownerchip of the instance, it is a dangerous class construction.

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      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