![]() |
2010-04-07
, 21:43
|
Posts: 17 |
Thanked: 42 times |
Joined on Feb 2010
|
#2
|
The Following 4 Users Say Thank You to StewartHolmes For This Useful Post: | ||
![]() |
2010-04-07
, 21:51
|
Posts: 388 |
Thanked: 842 times |
Joined on Sep 2009
@ Finland
|
#3
|
![]() |
2010-04-07
, 21:55
|
|
Posts: 1,296 |
Thanked: 1,773 times |
Joined on Aug 2009
@ Budapest, Hungary
|
#4
|
![]() |
2010-05-19
, 06:48
|
Posts: 124 |
Thanked: 10 times |
Joined on Jan 2007
@ Italy
|
#5
|
The difference in your code is that in one, you create the QNetworkManager on the stack
QNetworkManager m;
and in the other you create it on the heap
m = new QNetworkManager();
When you create on the stack, at the end of the current closure, the memory will be freed, and the ~QNetworkManager() method called.
![]() |
2010-05-19
, 06:54
|
Posts: 190 |
Thanked: 129 times |
Joined on Mar 2010
@ Bavaria, Germany
|
#6
|
// returns dead pointer (obj dies when leaving function) QObject* function() { QObject obj; return &obj; } // returns valid pointer (the return value has to be "delete"d) QObject* function() { return new QObject; }
![]() |
2010-05-19
, 07:07
|
Posts: 124 |
Thanked: 10 times |
Joined on Jan 2007
@ Italy
|
#7
|
void function() { QObject* obj = new QObject; obj->doSomething() }
![]() |
2010-05-19
, 07:20
|
Posts: 190 |
Thanked: 129 times |
Joined on Mar 2010
@ Bavaria, Germany
|
#8
|
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:
is better use stack allocation this time?Code:void function() { QObject* obj = new QObject; obj->doSomething() }
![]() |
2010-05-19
, 07:28
|
Posts: 19 |
Thanked: 7 times |
Joined on Jan 2010
|
#9
|
![]() |
2010-05-19
, 07:33
|
Posts: 124 |
Thanked: 10 times |
Joined on Jan 2007
@ Italy
|
#10
|
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.
I'm pretty new to Qt development, and I've just encountered something that I wasn't able to figure out for hours.
How come that the following code doesn't work:
Can anyone elaborate on this?
You should follow me on Twitter!
Apps: Puzzle Master, IRC Chatter for Harmattan, IRC for Sailfish