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. QObjects who's the parent and Who's the child really !! [memory mangement issue] [SOLVE]

QObjects who's the parent and Who's the child really !! [memory mangement issue] [SOLVE]

Scheduled Pinned Locked Moved Unsolved General and Desktop
qobjectmemory managmen
11 Posts 7 Posters 3.6k 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.
  • K Offline
    K Offline
    Kamal Badi
    wrote on 1 Feb 2016, 08:42 last edited by Kamal Badi 2 Jan 2016, 10:18
    #1

    hi all experts,

    i have issue in understanding some cases in Qt Object Tree, down below am going to demonstrate it step by step

    #include <QApplication>
    #include "QDebug"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
      QString  *x=new QString("test");
      QString  *y;
    
    //the first issue is   
     qDebug()<<*x; //it return  "test"
    delete x;
      y=new QString("test 2");
    qDebug()<<*x; // it return  "test 2" !! SO HOW THAT'S IS HAPPEN
    
    //the second issue is
      y=x;
    //and that is mean x is Parent of the y right ! so when 
      delete y;
      qDebug()<<*x; // it return " " , it also deleted !!
    
        return a.exec();
    
    }
    

    so the example above explain that's no such thing call Parent and child ! if you assign object to other object they become one thing ...
    and if you delete object and create new object the deleted object will take the value of the new one and it's not deleted in real, it still here and you can use it again !!
    explain am i right or i misunderstand something here ??
    and if its right , should it suppose to be like that ?
    thanks in advance ...

    Dook

    T J 2 Replies Last reply 1 Feb 2016, 08:58
    0
    • K Kamal Badi
      1 Feb 2016, 08:42

      hi all experts,

      i have issue in understanding some cases in Qt Object Tree, down below am going to demonstrate it step by step

      #include <QApplication>
      #include "QDebug"
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
        QString  *x=new QString("test");
        QString  *y;
      
      //the first issue is   
       qDebug()<<*x; //it return  "test"
      delete x;
        y=new QString("test 2");
      qDebug()<<*x; // it return  "test 2" !! SO HOW THAT'S IS HAPPEN
      
      //the second issue is
        y=x;
      //and that is mean x is Parent of the y right ! so when 
        delete y;
        qDebug()<<*x; // it return " " , it also deleted !!
      
          return a.exec();
      
      }
      

      so the example above explain that's no such thing call Parent and child ! if you assign object to other object they become one thing ...
      and if you delete object and create new object the deleted object will take the value of the new one and it's not deleted in real, it still here and you can use it again !!
      explain am i right or i misunderstand something here ??
      and if its right , should it suppose to be like that ?
      thanks in advance ...

      T Offline
      T Offline
      the_
      wrote on 1 Feb 2016, 08:58 last edited by
      #2

      @Kamal-Badi said:

      hi all experts,

      i have issue in understanding some cases in Qt Object Tree, down below am going to demonstrate it step by step

      #include <QApplication>
      #include "QDebug"
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
        QString  *x=new QString("test");
        QString  *y;
      
      //the first issue is   
       qDebug()<<*x; //it return  "test"
      delete x;
        y=new QString("test 2");
      qDebug()<<*x; // it return  "test 2" !! SO HOW THAT'S IS HAPPEN
      
      //the second issue is
        y=x;
      //and that is mean x is Parent of the y right ! so when 
        delete y;
        qDebug()<<*x; // it return " " , it also deleted !!
      
          return a.exec();
      
      }
      

      You are dealing with pointers here.
      It is possible that y points to the same address a x after delete and new.

      Second you assign the pointers address to another variable, if you print x instead of *x you will get the pointers address. so if you delete y you free the memeory at the address that is stored in y which is the same address as stored in x after assigning y = x;

      -- No support in PM --

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 1 Feb 2016, 09:10 last edited by
        #3

        Hi
        Parent and child applies to
        QObjects and QString is not that/has as base.

        The child and parent relation is often created via the constructor that takes the parent
        as parameter.
        QPushButton *but= new QPushButton(this);
        "This" often being main window or some other widget that will become the parent and
        own the button.

        Your code just do funky stuff to pointers :)

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kamal Badi
          wrote on 1 Feb 2016, 09:11 last edited by
          #4

          hi @the_ thanks for replying ..
          first issue i delete the pointer x it must not point to any thing in the memory , so when create new y after i delete x , y stored in the same location of x , so x assign to new value here...

          second am talking a bout who's the parent i mean if i delete any one of x or y the second is deleted with it so who's the parent here ??
          they say

          When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is.

          and by this way reference's object are working so there is no need for such thing call static ...

          Dook

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 1 Feb 2016, 09:12 last edited by
            #5

            Hi,

            You example doesn't use Qt's parent child feature at all. To start, QString is not a QObject. Not all Qt classes are QObject derived.

            Assigning a pointer to another pointer doesn't create any relation, you just overwrote the value of y with the value of x thus you created a memory leak because now the memory allocated to y is unreachable since the pointer value has been replaced.

            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
            0
            • G Offline
              G Offline
              gyll
              wrote on 1 Feb 2016, 09:14 last edited by gyll 2 Jan 2016, 09:17
              #6

              You don't understand the basics.

              1. when you delete an object in C++ it means that the corresponding memory is "freed", but the memory may still retain its prior value until another object is allocated there.

              2. the concept of parent and child is not a C++ concept, and it depends on each particular application. In Qt, some, but not all, object constructors take a parameter that Qt calls "parent", and if you pass a pointer to the constructor then that pointer represents the object's parent; e.g.:

              QObject *firstObject = new QObject;
              QObject *childOfFirstObject = new QObject(firstObject); // this makes 'childOfFirstObject' a child of 'firstObject'
              

              In particular, QString is a Qt object that does not provide a constructor allowing you to make it a child of another object, such that a QString object cannot be the child of any other object (in Qt parlance).

              Finally, what you do when you assign 'y=x' has absolutely nothing to do with a parent/child relationship; you just make an ordinary assignment. It's like saying that if you assign int j = i you make j a child of i, which is nonsense.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Kamal Badi
                wrote on 1 Feb 2016, 09:45 last edited by
                #7

                sorry i use the QString to show you guys the logic ..
                thanks i get it the parent and the child relation :)

                my Question here is
                when i Said y=x;
                thats mean they pointed to the same location in the memory right;
                so when i delete y it should delete the pointer not the value , why it delete the value of x ?

                Dook

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 1 Feb 2016, 09:50 last edited by
                  #8

                  With y = x both y and x have the same value thus point to the same memory region, deleting a pointer means that you are freeing the associated memory thus y and x point to the same now invalid memory region.

                  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
                  0
                  • K Kamal Badi
                    1 Feb 2016, 08:42

                    hi all experts,

                    i have issue in understanding some cases in Qt Object Tree, down below am going to demonstrate it step by step

                    #include <QApplication>
                    #include "QDebug"
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                    
                      QString  *x=new QString("test");
                      QString  *y;
                    
                    //the first issue is   
                     qDebug()<<*x; //it return  "test"
                    delete x;
                      y=new QString("test 2");
                    qDebug()<<*x; // it return  "test 2" !! SO HOW THAT'S IS HAPPEN
                    
                    //the second issue is
                      y=x;
                    //and that is mean x is Parent of the y right ! so when 
                      delete y;
                      qDebug()<<*x; // it return " " , it also deleted !!
                    
                        return a.exec();
                    
                    }
                    

                    so the example above explain that's no such thing call Parent and child ! if you assign object to other object they become one thing ...
                    and if you delete object and create new object the deleted object will take the value of the new one and it's not deleted in real, it still here and you can use it again !!
                    explain am i right or i misunderstand something here ??
                    and if its right , should it suppose to be like that ?
                    thanks in advance ...

                    J Offline
                    J Offline
                    JKSH
                    Moderators
                    wrote on 1 Feb 2016, 10:07 last edited by JKSH 2 Jan 2016, 10:09
                    #9

                    @Kamal-Badi said:

                    delete x;
                    // ...
                    qDebug()<<*x;
                    

                    This is illegal. You cannot trust the results of this experiment.

                    Remember this golden rule of C++: DO NOT USE A POINTER AFTER YOU DELETE IT!!!

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    0
                    • jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 1 Feb 2016, 10:12 last edited by
                      #10
                      delete x;
                      y=new QString("test 2");
                      qDebug()<<*x; // it return  "test 2" !! SO HOW THAT'S IS HAPPEN
                      

                      You freed the memory via delete x;
                      Then you allocated memory via y=new QString("test 2");
                      In this case it looks like it just allocated the same memory you freed before, that's why qDebug()<<*x; prints "test 2".
                      This is just luck. Usually your application will crash if you do such bad things!
                      But as JKSH sad NEVER dereference a pointer which you already freed via delete!

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

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        Kamal Badi
                        wrote on 1 Feb 2016, 10:16 last edited by
                        #11

                        Thanks all.
                        now i got it all clear :)

                        Dook

                        1 Reply Last reply
                        0

                        1/11

                        1 Feb 2016, 08:42

                        • Login

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