Firsts of all,It has been said before in other posts - Qt is not a garbage collector ( it is C++ after all ), so it only takes care of certain objects to be deleted. A QObject normally deletes all it's children.
Second of all, you should take a C++ book ( doesn't matter which one at this point ), and read it very carefully, cover to cover. You can try this link:
http://www.qtcentre.org/forum/f-gene...-books-29.html
Anyway, here are you answers:
1. The destructor of your class should be:
Foo::~Foo()
{
if( cameras )
{
while( cameras->count() )
{
Camera* c = cameras->takeAt(0);
delete c;
}
}
delete cameras;
cameras = NULL;
}
Foo::~Foo()
{
if( cameras )
{
while( cameras->count() )
{
Camera* c = cameras->takeAt(0);
delete c;
}
}
delete cameras;
cameras = NULL;
}
To copy to clipboard, switch view to plain text mode
Since the other QList is static, you can't delete it in the destructor, because other class instances can still exists and be using it. So you should have a static member that disposes the list at the end of your app:
void Foo::DisposeAreas()
{
if( areas )
{
while( areas->count() )
{
Area* a = areas->takeAt(0);
delete a;
}
}
delete areas;
areas = NULL;
}
void Foo::DisposeAreas()
{
if( areas )
{
while( areas->count() )
{
Area* a = areas->takeAt(0);
delete a;
}
}
delete areas;
areas = NULL;
}
To copy to clipboard, switch view to plain text mode
delete[] is normally used for array allocated on the heap.
Something like:
Foo *fooArray = new Foo()[20];
...
delete[] fooArray;
This will cause 20 Foo destructors to be called.
Foo *fooArray = new Foo()[20];
...
delete[] fooArray;
This will cause 20 Foo destructors to be called.
To copy to clipboard, switch view to plain text mode
2. Yes.
3. If the parent of the objects inside a QList is the QList itself, then the QObjects inside will be deleted, otherwise no.
This is because:
QObject::~QObject()
Destroys the object, deleting all its child objects. All signals to and from the object are automatically disconnected, and any pending posted events for the object are removed from the event queue. However, it is often safer to use deleteLater() rather than deleting a QObject subclass directly.
Warning: All child objects are deleted. If any of these objects are on the stack or global, sooner or later your program will crash. We do not recommend holding pointers to child objects from outside the parent. If you still do, the destroyed() signal gives you an opportunity to detect when an object is destroyed.
Warning: Deleting a QObject while pending events are waiting to be delivered can cause a crash. You must not delete the QObject directly if it exists in a different thread than the one currently executing. Use deleteLater() instead, which will cause the event loop to delete the object after all pending events have been delivered to it.
1. No advantage or disadvantage ( as I said, read a good book with good examples ).
2. Lines 9-11.
That will not work and it will crash a program .You cannot delete p_f because it points to something allocated on the stack, and not in the heap.
Anyway, let's assume you did this:
Foo f();
Foo *p_f = new Foo();
delete p_f; //does 'f' gets deleted here or can i continue using it?
Foo f();
Foo *p_f = new Foo();
delete p_f; //does 'f' gets deleted here or can i continue using it?
To copy to clipboard, switch view to plain text mode
In this case p_f gets deleted and remains an invalid pointer ( points to an invalid memory location ). Therefore, no matter for what you are using it next, it has to be allocated, or you will get a mem violation.
Another error in the function createFoo().
Even if you reallocate p_f above, Foo f is local in createFoo, therefore it will be deleted when the function exits its scope( returns) - you can test this by adding a breakpoint in ~Foo right before this function returns. Therefore p_f will remain allocated but will contain an invalid object.
Actually here is a good example of why you should use a pointer - because a pointer is allocated on the heap, not on the stack, and it only gets deallocated by delete, it does not depend by the scope in which it was allocated
void createFoo(Foo* p_f)
{
Foo *f = new Foo();
if( p_f ) // do this, or don't allocate p_f again after deleting it, in main
{
delete p_f;
p_f = NULL;
}
p_f = f;
}
void createFoo(Foo* p_f)
{
Foo *f = new Foo();
if( p_f ) // do this, or don't allocate p_f again after deleting it, in main
{
delete p_f;
p_f = NULL;
}
p_f = f;
}
To copy to clipboard, switch view to plain text mode
Hope this helps.
Regards
Bookmarks