PDA

View Full Version : Does QList use copy constructor when some element append to it?



indomie_seleraku
17th December 2010, 09:19
does QList copy the element?
or does it keep only "the memory address -- reference"?
(so do i need to free my "pointer" element?)

eg.


QList list;
Element *e;
while (query.next()) {
e = new Element();
...
list << *e;
delete e; // required?
}

franz
17th December 2010, 09:27
It copies the element. In your case, the better code would be:

QList list;
while (query.next()) {
Element e;
...
list << e;
}

It's documented (http://doc.trolltech.com/latest/containers.html#assignable-data-type).

stampede
17th December 2010, 09:32
Have you looked into docs ? There is clearly stated:

Internally, QList<T> is represented as an array of pointers to items of type T. If T is itself a pointer type or a basic type that is no larger than a pointer, or if T is one of Qt's shared classes, then QList<T> stores the items directly in the pointer array.

What's the purpose of having

QList<Element> list;
rather than

QList<Element*> list;
?
This way you can delete objects after you're done using them. It's better design, IMHO, than this:

e = new Element();
...
list << *e;
delete e;
Here you are creating element, store it in list, so its obvious that you're going to use it in the future, but in the next line you are deleting it, as if you don't need it anymore.
Just store pointers and delete them when you are done.

franz
17th December 2010, 09:39
e = new Element();
...
list << *e;
delete e;
Here you are creating element, store it in list, so its obvious that you're going to use it in the future, but in the next line you are deleting it, as if you don't need it anymore.
Just store pointers and delete them when you are done.

Actually that code stores a copy of *e, not a copy of e. The local object pointed at is deleted, not the one in the list. Those are two different instances. The fact that QList might be using a pointer to an object internally is irrelevant here.

indomie_seleraku
17th December 2010, 10:01
Here you are creating element, store it in list, so its obvious that you're going to use it in the future, but in the next line you are deleting it, as if you don't need it anymore.
Just store pointers and delete them when you are done.

it's just an example :)
actually, i need to put the something in a collection class "that save only the pointer", not cloning it...
like saving a Tcp Socket... (from what i learned here... QList is not suitable collection class for this, cmiiw)



delete e; // required?

see the comment i put... i wondered if i need those line before :)
i expect its only to keep pointer, not copying it...
but responsible for freeing when the list's desctructor is called


simply, i looking for replacement of TObjectList in Pascal
it keep only pointer
but responsible for freeing also

thanks for all reply :)

franz
17th December 2010, 10:19
If you store a pointer in a list, don't delete the object. You will have a dangling pointer.

QList is not responsible for deleting objects. You can use QSharedPointer<> for that, provided that you don't setParent() on your QObjects:


QList<QSharedPointer<QTcpSocket> > list;
list << QSharedPointer<QTcpSocket>(new QTcpSocket);
list.last()->doTheConnectStuff();

indomie_seleraku
17th December 2010, 10:52
thank you,


QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.

what does "out of scope" means here?

outside of { ... } it's curly brace?


{
QTcpSocket *socket = new QTcpSocket();
QSharedPointer<QTcpSocket> socketHolder(socket);

} // socket will auto deleted here?


i still confuse with your example (put inside parameter)
sorry for my slow brain... need to break down


{
QTcpSocket *socket = new QTcpSocket();
QSharedPointer<QTcpSocket> socketHolder(socket);
list << socketHolder;
} // **


** socketHolder already out side of scope, does it delete socket?
so does it means QSharedPointer that get copied inside list point to invalid address?

or

(since it named 'Shared')
it will not delete the object if there other QSharedPointer still holding the same object.
that's means only the last QSharedPointer will responsible for deleting?

franz
17th December 2010, 20:47
This:

it will not delete the object if there other QSharedPointer still holding the same object.
that's means only the last QSharedPointer will responsible for deleting?

QSharedPointer deletes the object when the last reference has gone out of scope. If you add one to a QList, there is still a reference in scope.

Reading material: QSharedPointer, Count with me: How many smart pointer classes does Qt have? (http://labs.qt.nokia.com/2009/08/25/count-with-me-how-many-smart-pointer-classes-does-qt-have/).