View Single Post
Posts: 190 | Thanked: 129 times | Joined on Mar 2010 @ Bavaria, Germany
#8
Originally Posted by saxen View Post
thanks for reply! what if a pointer(heap allocation) is declared in a method but not returned?? it's not dead, but also out of scope, so not usefull?

i mean:
Code:
void function()
{
  QObject* obj =  new QObject;
  obj->doSomething()
}
is better use stack allocation this time?
Now your code is a memory leak. The Object still exists on the heap but you can never find it again to delete.

But QObject's have an exception with the deleting: Every QObject deletes it's children when it is deleted. So always do "new QObject(parent)" when possible, then you don't have to care about memory freeing.