Active Topics

 


Reply
Thread Tools
Posts: 190 | Thanked: 129 times | Joined on Mar 2010 @ Bavaria, Germany
#71
Originally Posted by realsportcars View Post
when an object on the heap exit from scope or have no pointer referencing it, is true or not that this object will be deleted automatically? i read something about this while reading Qt documentation but im not really sure...can you help me?
No. Objects on the stack will die when leaving scope, objects on the heap will remain in your memory forever until you delete them yourself or the application quits. C++ does not do any reference counting.

PS: In 99% of all cases, you never have to create QStrings on the heap
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#72
If you create an object on the stack it will have its destructor called when you leave the scope it's defined in.

When you allocate on the heap, you need to call delete to deallocate the object.

EDIT:

Meh, what gri said. I didn't see the page pointer.
 
Posts: 51 | Thanked: 32 times | Joined on Jan 2010
#73
Well there are exceptions to this rule at least when using Qt.

First, if possible use objects on stack. (you don't have do delete them).QStrings are implicit shared, so having them on stack even when that means a copy, is no big issue.

Then, if your object is a QObject derived one, you can give it a parent. And then it automatically gets deleted with the parent object. (For example Buttons on a QDialog. You don't have nor should you delete those objects.

If unsure you should get sure, but you also can set the pointer to 0 after you deleted the object. delete 0 is safe and does nothing, but double deleting is bad.
 
Posts: 401 | Thanked: 724 times | Joined on May 2010 @ Italy
#74
Thank you for the explain. I also read some documentation about Qt and parents system for deleting object.

I will take care about it for the future and for next release
 
Reply


 
Forum Jump


All times are GMT. The time now is 03:25.