PDA

View Full Version : About new and delete?



yangyunzhao
29th June 2009, 03:18
First of all ,sorry for my poor English.

On the internet , I often saw somebody says "You can new a clas without delete it if it inherits from QObject"

But in the book "C++ GUI Programming with Qt 4 Second Edition", I saw "When we create an object (a widget, validator, or any other kind) with a parent, the parent adds the object to the list of its children. When the parent is deleted, it walks through its list of children and deletes each child. The children themselves then delete all of their children, and so on recursively until none remain."

So I have a quesstion. Some object such as QString ,it inherits from QObject but have no interface.When I new a QString
1、I think it has no parent.Am I right?
2、Need I delete it?

zhq306
29th June 2009, 06:12
QString is not inherit form QObject.

yangyunzhao
29th June 2009, 06:43
Thanks for your help.I make a mis-example.

I want to know, When I new a Object( which inherits from QObject but has no parent ), Need I delete it?

aamer4yu
29th June 2009, 06:49
But usually we dont create QString on heap using new, do we ?
So I dont think there is such need to worry about stack object getting destroyed

andy.fillebrown
29th June 2009, 07:26
first off, this is really easy to find out on your own -- write your own class derived from QObject and put traces in the destructor to see if it ever gets deleted.

secondly, "no" -- objects of type QObject do not delete themselves automatically unless they are child objects of a parent object also derived from QObject. Whoever told you this is wrong -- or you misunderstood what they were trying to tell you.

---

an example...


QObject *object1 = new QObject;
QObject *object2 = new QObject(object1);

...object2 does not need to be deleted explicitly because object1 is its parent -- but object1 does need to be deleted explicitly because it has no parent.

So when you call "delete" on object1, object2 is also deleted. Conversely, you should NOT call "delete" on object2 unless you remove it from object1's ownership tree.

OTOH if you did this...


QObject *object1 = new QObject;
QObject *object2 = new QObject;

...then BOTH of them need to be deleted explicitly since neither one has a parent specified in the constructor.

---

so to be perfectly clear, going back to the first example...


QObject *object1 = new QObject;
QObject *object2 = new QObject(object1);

[...] // do what you will with object1 and object2...

delete object1;

[...] // ...don't use object1 and object2 because they're both gone.

---

Since most QObjects (if not all of them) in your apps will be created after the main QApplication object is allocated, you can use the QApplication object as the first parent so you never have orphaned objects derived from QObject...

another example (given "MyClassFactory" and "MyClass" both derive from QObject)...


MyClass *MyClassFactory::createMyClass()
{
return new MyClass(this); // sets myFactory as myClass's parent in "main"...
}

int main(int argc, char **argv)
{
// init app...

QApplication *myApp = new QApplication(argc, argv);
MyClassFactory *myFactory = new MyClassFactory(myApp);
MyClass *myClass1 = myFactory->createMyClass();
MyClass *myClass2 = myFactory->createMyClass();
// myClass3, myClass4, etc, etc...

// run app...

app.exec();

// cleanup app...

/* deleting myApp also deletes myFactory and every object created with MyClassFactory::createMyClass() */

delete myApp;

//... done

return 0;
}

Scorp2us
29th June 2009, 20:31
The implicitly shared classes use reference counting. This means that if you make one dynamically (with new) you have to delete it when you are done with it. If you make it statically then Qt handles it.

The QObject-derived classes will delete their children automatically when the parent is deleted. Works with new or on the stack.

Objects without a parent (NULL parent pointer) are your responsibility to delete, if not statically created.

wysota
29th June 2009, 23:26
The implicitly shared classes use reference counting.
These are mutable - they don't inherit QObject.


If you make it statically then Qt handles it.
No, Qt has nothing to do with this. The compiler is responsible for deleting stack based objects.


The QObject-derived classes will delete their children automatically when the parent is deleted. Works with new or on the stack.
No, if you create a QObject on the stack and give it a parent, there is a 50% chance deleting the parent will lead to a segfault. Always create parented QObjects on heap.

Scorp2us
30th June 2009, 02:59
Er, um, yeah. That's what I meant. I guess that's not what I said.

wysota
30th June 2009, 10:37
Er, um, yeah. That's what I meant. I guess that's not what I said.

I'm correcting such things only because uncorrected they lead to myths that are later referenced to as absolute truths ("Hey, Qt is stupid, it deleted my object twice. How lame is that? They write about it here on Qt Centre <and here comes the reference to this thread>").

yangyunzhao
1st July 2009, 08:06
Thanks of all!!
Especially andy.fillebrown at #5.
I'm just a rookie.