Reply
Thread Tools
Venemo's Avatar
Posts: 1,296 | Thanked: 1,773 times | Joined on Aug 2009 @ Budapest, Hungary
#1
Hi,

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:
Code:
QNetworkRequest request(QUrl("http://venemo.net/Default.aspx"));
QNetworkAccessManager manager(this);
connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
manager.get(request);
while the following does:
Code:
QNetworkRequest request(QUrl("http://venemo.net/Default.aspx"));
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
manager->get(request);
I think that both syntaxes SHOULD do the same, or at least, when I was at university, they did at my C++ course.

Can anyone elaborate on this?
 
Posts: 17 | Thanked: 42 times | Joined on Feb 2010
#2
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.
 

The Following 4 Users Say Thank You to StewartHolmes For This Useful Post:
Posts: 388 | Thanked: 842 times | Joined on Sep 2009 @ Finland
#3
It's not exactly the same. In first case the manager object gets automatically deleted when it goes out of scope (that is, when '}' is reached if I remember correctly).

In the second case the manager object (to which the pointer refers to) allocated with "new" never gets deleted unless "delete" is used on it. It results in memory leak if you forget to delete it.
 

The Following 2 Users Say Thank You to hqh For This Useful Post:
Venemo's Avatar
Posts: 1,296 | Thanked: 1,773 times | Joined on Aug 2009 @ Budapest, Hungary
#4
Thanks!

Indeed, those C++ courses were quite long ago.

I forgot that the QNetworkManager instance is destroyed after the function call ends, and it really shouldn't be destroyed because it is requred to call back the finished event.

I feel so ashamed now...
 
Posts: 124 | Thanked: 10 times | Joined on Jan 2007 @ Italy
#5
Originally Posted by StewartHolmes View Post
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.
Sorry for (maybe) stupid question...i'm new with c++! when using one instead of the other?
i mean, is it only a difference in term of memory allocation??
 
Posts: 190 | Thanked: 129 times | Joined on Mar 2010 @ Bavaria, Germany
#6
Objects on the stack don't need to be deleted.

Code:
// 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;
}
In case of the QNetworkManager this means: The request is started via the manager but the manager dies at the function end. So the request never comes back.
 
Posts: 124 | Thanked: 10 times | Joined on Jan 2007 @ Italy
#7
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?
 
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.
 
Posts: 19 | Thanked: 7 times | Joined on Jan 2010
#9
Originally Posted by saxen View Post
Code:
void function()
{
  QObject* obj =  new QObject;
  obj->doSomething()
}
This is called a memory leak
And my advice: if you ask yourself if heap or stack should be used, the stack is mostly the better solution.
 
Posts: 124 | Thanked: 10 times | Joined on Jan 2007 @ Italy
#10
Originally Posted by gri View Post
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.
where "parent" could be the class containing it? such a widget?
so with this trick i don't need to call explicitly delete on every object but just on "main objects"?!

thanks for help! i'm missing java garbage collector
 
Reply


 
Forum Jump


All times are GMT. The time now is 22:47.