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 due to Qt Designer components/widgets
Qt 6.11 is out! See what's new in the release blog

Memory Leak due to Qt Designer components/widgets

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 6 Posters 1.3k Views 4 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.
  • Puppy BearP Offline
    Puppy BearP Offline
    Puppy Bear
    wrote on last edited by Puppy Bear
    #1

    HI, I'm now using Qt designer to create an application , however there's some memory leak according to vld,
    which shows that the components are not being deleted
    I traced in and shows:
    painter2 = new QPainter(this); is the reason of my promoted label

    void MyLabel::paintEvent(QPaintEvent *event)
    {
    
    	QLabel::paintEvent(event);   
    	QPainter painter(this);
    
    	painter2 = new QPainter(this);// Painter(this); 
    
    
    	painter.setPen(QPen(Qt::green, 2));
    	if (show) {
    		if (m_rectStartPoint != m_rectEndPoint && (mouse_pressed == true || mouse_released == true))
    		{
    			painter.drawRect(QRect(m_rectStartPoint, m_rectEndPoint));
    		}
    	}
    	else {
    		
    	}
    
    }
    

    I've checked and not sure is it a setparent problem, cause qlabel should already compiled and set parent automatically by Qt designer .ui file.

    (My main window and subwindow also have the same problem, set parents not working for them, am I supposed to delete them by myself? Thought they should be automatically deleted by Qt)

    Pl45m4P Pablo J. RoginaP SGaistS 3 Replies Last reply
    0
    • Puppy BearP Puppy Bear

      HI, I'm now using Qt designer to create an application , however there's some memory leak according to vld,
      which shows that the components are not being deleted
      I traced in and shows:
      painter2 = new QPainter(this); is the reason of my promoted label

      void MyLabel::paintEvent(QPaintEvent *event)
      {
      
      	QLabel::paintEvent(event);   
      	QPainter painter(this);
      
      	painter2 = new QPainter(this);// Painter(this); 
      
      
      	painter.setPen(QPen(Qt::green, 2));
      	if (show) {
      		if (m_rectStartPoint != m_rectEndPoint && (mouse_pressed == true || mouse_released == true))
      		{
      			painter.drawRect(QRect(m_rectStartPoint, m_rectEndPoint));
      		}
      	}
      	else {
      		
      	}
      
      }
      

      I've checked and not sure is it a setparent problem, cause qlabel should already compiled and set parent automatically by Qt designer .ui file.

      (My main window and subwindow also have the same problem, set parents not working for them, am I supposed to delete them by myself? Thought they should be automatically deleted by Qt)

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #6

      Hi,

      @Puppy-Bear said in Memory Leak due to Qt Designer components/widgets:

      painter2 = new QPainter(this);// Painter(this);

      You are allocating a new QPainter on each call to paintEvent which can happen quite a lot.

      This wrong on two levels:

      • you never delete that object
      • it's only useful in the paint event method so there's no need to allocate it on the heap.

      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
      2
      • Puppy BearP Puppy Bear

        HI, I'm now using Qt designer to create an application , however there's some memory leak according to vld,
        which shows that the components are not being deleted
        I traced in and shows:
        painter2 = new QPainter(this); is the reason of my promoted label

        void MyLabel::paintEvent(QPaintEvent *event)
        {
        
        	QLabel::paintEvent(event);   
        	QPainter painter(this);
        
        	painter2 = new QPainter(this);// Painter(this); 
        
        
        	painter.setPen(QPen(Qt::green, 2));
        	if (show) {
        		if (m_rectStartPoint != m_rectEndPoint && (mouse_pressed == true || mouse_released == true))
        		{
        			painter.drawRect(QRect(m_rectStartPoint, m_rectEndPoint));
        		}
        	}
        	else {
        		
        	}
        
        }
        

        I've checked and not sure is it a setparent problem, cause qlabel should already compiled and set parent automatically by Qt designer .ui file.

        (My main window and subwindow also have the same problem, set parents not working for them, am I supposed to delete them by myself? Thought they should be automatically deleted by Qt)

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #2

        @Puppy-Bear

        Hi, QObjects get destroyed together with their parent.

        • https://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

        The parent has nothing to do with whether you use an UI file or not.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #3

          QPainter does not subclass from QObject, so there won't be something like "parent" to it.

          Pl45m4P 1 Reply Last reply
          1
          • Puppy BearP Puppy Bear

            HI, I'm now using Qt designer to create an application , however there's some memory leak according to vld,
            which shows that the components are not being deleted
            I traced in and shows:
            painter2 = new QPainter(this); is the reason of my promoted label

            void MyLabel::paintEvent(QPaintEvent *event)
            {
            
            	QLabel::paintEvent(event);   
            	QPainter painter(this);
            
            	painter2 = new QPainter(this);// Painter(this); 
            
            
            	painter.setPen(QPen(Qt::green, 2));
            	if (show) {
            		if (m_rectStartPoint != m_rectEndPoint && (mouse_pressed == true || mouse_released == true))
            		{
            			painter.drawRect(QRect(m_rectStartPoint, m_rectEndPoint));
            		}
            	}
            	else {
            		
            	}
            
            }
            

            I've checked and not sure is it a setparent problem, cause qlabel should already compiled and set parent automatically by Qt designer .ui file.

            (My main window and subwindow also have the same problem, set parents not working for them, am I supposed to delete them by myself? Thought they should be automatically deleted by Qt)

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #4

            @Puppy-Bear said in Memory Leak due to Qt Designer components/widgets:

            painter2 = new QPainter(this);// Painter(this);

            @Bonnie is right regarding parenthood. Please take a look at the documentation.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mchinand
              wrote on last edited by
              #5

              @Puppy-Bear said in Memory Leak due to Qt Designer components/widgets:

              painter2 = new QPainter(this);// Painter(this);

              What's the reason for this line? You already created a QPainter object on the stack (painter) and that is the QPainter object that you used to perform paint operations.

              1 Reply Last reply
              0
              • Puppy BearP Puppy Bear

                HI, I'm now using Qt designer to create an application , however there's some memory leak according to vld,
                which shows that the components are not being deleted
                I traced in and shows:
                painter2 = new QPainter(this); is the reason of my promoted label

                void MyLabel::paintEvent(QPaintEvent *event)
                {
                
                	QLabel::paintEvent(event);   
                	QPainter painter(this);
                
                	painter2 = new QPainter(this);// Painter(this); 
                
                
                	painter.setPen(QPen(Qt::green, 2));
                	if (show) {
                		if (m_rectStartPoint != m_rectEndPoint && (mouse_pressed == true || mouse_released == true))
                		{
                			painter.drawRect(QRect(m_rectStartPoint, m_rectEndPoint));
                		}
                	}
                	else {
                		
                	}
                
                }
                

                I've checked and not sure is it a setparent problem, cause qlabel should already compiled and set parent automatically by Qt designer .ui file.

                (My main window and subwindow also have the same problem, set parents not working for them, am I supposed to delete them by myself? Thought they should be automatically deleted by Qt)

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                Hi,

                @Puppy-Bear said in Memory Leak due to Qt Designer components/widgets:

                painter2 = new QPainter(this);// Painter(this);

                You are allocating a new QPainter on each call to paintEvent which can happen quite a lot.

                This wrong on two levels:

                • you never delete that object
                • it's only useful in the paint event method so there's no need to allocate it on the heap.

                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
                2
                • B Bonnie

                  QPainter does not subclass from QObject, so there won't be something like "parent" to it.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #7

                  @Bonnie said in Memory Leak due to Qt Designer components/widgets:

                  QPainter does not subclass from QObject, so there won't be something like "parent" to it.

                  oops... forgot about that :)

                  Yeah, the usual way is to not new a QPainter in paintEvent, but allocate on stack, simply use it and let it go out of scope (like shown in QPainter documentation examples).


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  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