Results 1 to 5 of 5

Thread: Correctly destruction of child objects inside stack object

  1. #1
    Join Date
    Jan 2015
    Posts
    15
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Correctly destruction of child objects inside stack object

    Hi all, i'm trying to solve this problem.

    I have a Qt object that does not inherit from QObject:
    Qt Code:
    1. class A
    2. {
    3. public:
    4. A();
    5. ~A();
    6. B * bItem() const;
    7. C * cItem() const;
    8. QList<D *> dList() const;
    9. void addDItem(D*item);
    10. private:
    11. B * m_bItem;
    12. C * m_cItem;
    13. QList<D*> m_dList;
    14. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "a.h"
    2.  
    3. A::A(){
    4. this->m_bItem = new B;
    5. this->m_cItem = new C;
    6. }
    7.  
    8. A::~A(){
    9. }
    10.  
    11. C * A::cItem() const{
    12. return this->m_cItem;
    13. }
    14.  
    15. B *A::bItem() const{
    16. return this->m_bItem;
    17. }
    18.  
    19. QList<D*> A::dList() const{
    20. return this->m_dList;
    21. }
    22.  
    23. void A::addDItem(D*item);{
    24. this->m_dList.append(item);
    25. }
    To copy to clipboard, switch view to plain text mode 
    I created an object A:
    Qt Code:
    1. A a;
    To copy to clipboard, switch view to plain text mode 
    and i'm using it like a parameter in a SIGNAL\SLOT connection like this
    Qt Code:
    1. connect( obj, SIGNAL( sig_message( A ) ), obj2, SLOT( slot_message( A ) ) );
    To copy to clipboard, switch view to plain text mode 
    All working fine in the signal\slot mechanism and i can use the A param without problems.
    I saw also, when the scope of A param is finished, that the destructor of class A is called correctly.
    My only question\doubt is:

    how can i destroy the objects ( m_bItem , m_cItem and the list m_dList ) inside A?

    They are pointers(and a list of objects pointers).
    If use the delete function on those pointers the application crash.(during the emitting signal, maybe the destruction remove the object during the copy...).

    Am I using a wrong pattern?

    Any suggestion is appreciated.
    Thanks in advance.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Correctly destruction of child objects inside stack object

    Use QSharedPointer instead normal pointer.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Correctly destruction of child objects inside stack object

    Quote Originally Posted by papercut87 View Post
    how can i destroy the objects ( m_bItem , m_cItem and the list m_dList ) inside A?
    You created them with "new" so you need to call "delete" unless they are QObject derived and have a parent
    For a container like QList you can use qDeleteAll() to call delete on all elements.

    Quote Originally Posted by papercut87 View Post
    They are pointers(and a list of objects pointers).
    If these are QObject derived and have a parent then the parent will delete them.

    Quote Originally Posted by papercut87 View Post
    If use the delete function on those pointers the application crash.(during the emitting signal, maybe the destruction remove the object during the copy...).
    Because they are deleted multiple times because you have multiple copies of that A object and each executes the same delete on the same pointers.

    This is because your A class does not have a copy constructor, so the C++ compiler generates the standard one which does member-by-member copy.
    A member-by-member copy of a pointer just ends up with the same pointer in two A objects.

    There is a couple of approaches:

    - as Lesiok already mentioned by using smart pointers for either the members or for the instance of A that passes through the signal connection
    - passing A as a reference or pointer through the signal connection
    - implementing a copy constructor and assignment operator in A that creates deep copies of the members

    Cheers,
    _

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Correctly destruction of child objects inside stack object

    - passing A as a reference or pointer through the signal connection
    This immediately came to mind when I saw your code. By defining your signal and slots as taking an "A" argument and not an "A*" or "A&", your code is making a new instance of the original A object. As anda_skoa says, because you don't have a copy constructor defined, the default copy constructor does a member-by-member copy of the original "A" instance. In this case, it is simply copying the pointer values themselves, not the objects the pointers refer to. When your signal finishes execution, the copy of A made in the signal is deleted. If you delete the pointers in A's destructor, then the original A is left with "dangling pointers" - pointers to objects that have been deleted and are no longer valid. Next time you go to use one of those pointers, kapow!
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jan 2015
    Posts
    15
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Correctly destruction of child objects inside stack object

    Thanks to all men!

    I will try with one of the different solutions that you seggested me.
    I was sure that there was something wrong with my current implementation.

Similar Threads

  1. How to call a function in the mother object from a child object?
    By Momergil in forum General Programming
    Replies: 4
    Last Post: 18th December 2011, 15:49
  2. Stack object on top of one other
    By Markus_AC in forum Qt Programming
    Replies: 3
    Last Post: 23rd September 2011, 11:05
  3. Replies: 3
    Last Post: 25th July 2010, 19:15
  4. Replies: 4
    Last Post: 11th July 2007, 04:21
  5. declaring objects inside Designer
    By mickey in forum Qt Tools
    Replies: 8
    Last Post: 14th March 2006, 17: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.