Results 1 to 3 of 3

Thread: Memory management questions (im new to Qt)

  1. #1
    Join Date
    May 2007
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Memory management questions (im new to Qt)

    Hi friends,

    --Qt Questions

    I'm currently starting Qt programming and i have some questions:

    Qt Code:
    1. class Foo
    2. {
    3. ...
    4. static QList<Area*> *areas;
    5. QList<Camera*> cameras;
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    {Considerations-> Camera and Area are normal classes, no Qt involved.}

    1)I have these two objects, should i (in the destructor) call delete or delete [] for them?

    2)Should i call delete for each Camera* and Area* object?

    3)What would change in the first and in the second answer if the Foo class was derivated from QObject?

    --Non-Qt questions

    These are questions that always haunted me and never got answered:

    1)What's the big catch in using pointers, i know some cases that pointers are good (like when we want to point something ) but when there's the option of choosing between pointer or non-pointer what parameters should I obey to choose one of them? Like:
    Qt Code:
    1. class Foo
    2. {
    3. Foo(){};
    4. ~Foo(){};
    5.  
    6. void callMe() { cout<<"im alive"; };
    7. }
    8.  
    9. void main()
    10. {
    11. Foo *f = new Foo(); //what are the advantages/disadivantages of using a pointer here instead of Foo f();
    12. f->callMe();
    13. delete f;
    14. return;
    15. }
    To copy to clipboard, switch view to plain text mode 

    2)
    Qt Code:
    1. class Foo
    2. {
    3. Foo(){};
    4. ~Foo(){};
    5. }
    6.  
    7. void main()
    8. {
    9. Foo f();
    10. Foo *p_f = &f;
    11. delete p_f; //does 'f' gets deleted here or can i continue using it?
    12.  
    13. createFoo(p_f);
    14. //is p_f still valid here? i mean, wasn't the object f destroyed after leaving createFoo function?
    15. return;
    16. }
    17.  
    18. void createFoo(Foo* p_f)
    19. {
    20. Foo f();
    21. p_f = &f;
    22. }
    To copy to clipboard, switch view to plain text mode 

    Thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Memory management questions (im new to Qt)

    Quote Originally Posted by scarvenger View Post
    1)I have these two objects, should i (in the destructor) call delete or delete [] for them?
    Neither. Call qDeleteAll(*areas), qDeleteAll(cameras) and delete areas.

    2)Should i call delete for each Camera* and Area* object?
    Yes. That's what qDeleteAll does.

    3)What would change in the first and in the second answer if the Foo class was derivated from QObject?
    Nothing.

    1)What's the big catch in using pointers, i know some cases that pointers are good (like when we want to point something ) but when there's the option of choosing between pointer or non-pointer what parameters should I obey to choose one of them?
    There is no easy answer to that question. In general if you have a big stack, you should avoid pointers if you can. But sometimes you just have to use pointers (like with Qt widgets).

    Like:
    Qt Code:
    1. class Foo
    2. {
    3. Foo(){};
    4. ~Foo(){};
    5.  
    6. void callMe() { cout<<"im alive"; };
    7. }
    8.  
    9. void main()
    10. {
    11. Foo *f = new Foo(); //what are the advantages/disadivantages of using a pointer here instead of Foo f();
    12. f->callMe();
    13. delete f;
    14. return;
    15. }
    To copy to clipboard, switch view to plain text mode 
    There are no disadvantages (apart from a slight overhead of the need to actually allocate the memory) and no advantages here.

    2)
    Qt Code:
    1. class Foo
    2. {
    3. Foo(){};
    4. ~Foo(){};
    5. }
    6.  
    7. void main()
    8. {
    9. Foo f();
    10. Foo *p_f = &f;
    11. delete p_f; //does 'f' gets deleted here or can i continue using it?
    To copy to clipboard, switch view to plain text mode 
    This will segfault - you're deleting the object twice - first by explicitely calling delete and then when main() returns the compiler will try to delete the object again.

    Qt Code:
    1. createFoo(p_f);
    2. //is p_f still valid here? i mean, wasn't the object f destroyed after leaving createFoo function?
    3. return;
    4. }
    To copy to clipboard, switch view to plain text mode 
    No, it's not valid.

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Memory management questions (im new to Qt)

    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:
    Qt Code:
    1. Foo::~Foo()
    2. {
    3. if( cameras )
    4. {
    5. while( cameras->count() )
    6. {
    7. Camera* c = cameras->takeAt(0);
    8. delete c;
    9. }
    10. }
    11. delete cameras;
    12. cameras = NULL;
    13. }
    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:
    Qt Code:
    1. void Foo::DisposeAreas()
    2. {
    3. if( areas )
    4. {
    5. while( areas->count() )
    6. {
    7. Area* a = areas->takeAt(0);
    8. delete a;
    9. }
    10. }
    11. delete areas;
    12. areas = NULL;
    13. }
    To copy to clipboard, switch view to plain text mode 

    delete[] is normally used for array allocated on the heap.
    Something like:
    Qt Code:
    1. Foo *fooArray = new Foo()[20];
    2. ...
    3. delete[] fooArray;
    4. 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.
    --Non-Qt questions
    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:
    Qt Code:
    1. Foo f();
    2. Foo *p_f = new Foo();
    3. 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

    Qt Code:
    1. void createFoo(Foo* p_f)
    2. {
    3. Foo *f = new Foo();
    4.  
    5. if( p_f ) // do this, or don't allocate p_f again after deleting it, in main
    6. {
    7. delete p_f;
    8. p_f = NULL;
    9. }
    10.  
    11. p_f = f;
    12. }
    To copy to clipboard, switch view to plain text mode 
    Hope this helps.

    Regards
    Last edited by marcel; 6th May 2007 at 08:46.

Similar Threads

  1. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 20:42
  2. Memory Problem with SIGNALS and SLOTS
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2007, 21:39
  3. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 15:40
  4. Memory management in QT
    By Gayathri in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2006, 08:21
  5. Qt related questions and thoughts about getting job
    By AlexKiriukha in forum General Discussion
    Replies: 4
    Last Post: 26th January 2006, 13:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.