QObjects who's the parent and Who's the child really !! [memory mangement issue] [SOLVE]
-
@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;
-
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 :)
-
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 sayand by this way reference's object are working so there is no need for such thing call static ...
-
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.
-
You don't understand the basics.
-
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.
-
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.
-
-
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 ? -
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. -
@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!!!
-
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! -
Thanks all.
now i got it all clear :)