Results 1 to 4 of 4

Thread: QSharedPointer does not handle multiple inheritance correctly?

  1. #1
    Join Date
    Feb 2010
    Posts
    3

    Default Re: QSharedPointer does not handle multiple inheritance correctly?

    Hello,

    I have detected a very annoying issue with QSharedPointer and multiple inheritance. The following code gives a simplified example of the situation in my application (using Qt 4.7.0 on 64 bit Linux):

    Qt Code:
    1. #include <QtCore>
    2.  
    3. class A
    4. {
    5. int dataA;
    6. public:
    7. virtual void funcA() { }
    8. };
    9.  
    10. class Interface
    11. {
    12. public:
    13. virtual void funcIf()=0;
    14. };
    15.  
    16. class B : public A, public Interface
    17. {
    18. public:
    19. virtual void funcA() { }
    20. virtual void funcIf() { }
    21. };
    22.  
    23. int main()
    24. {
    25. QSharedPointer<B> pB = QSharedPointer<B>(new B());
    26. QSharedPointer<Interface> pInterface = pB;
    27.  
    28. pB.clear();
    29. pInterface.clear();
    30.  
    31. return 0;
    32. }
    To copy to clipboard, switch view to plain text mode 

    When trying to run this program, I get a

    *** glibc detected *** ./qsharedptr_test: double free or corruption (out): 0x00000000017d0740 ***

    error. Valgrind says

    ==7749== Invalid free() / delete / delete[]
    ==7749== at 0x4C27A83: operator delete(void*) (vg_replace_malloc.c:387)
    ==7749== by 0x400BA4: main (in /home/cc/temp/qsharedptr_test/qsharedptr_test)
    ==7749== Address 0xa267840 is 16 bytes inside a block of size 24 alloc'd
    ==7749== at 0x4C28973: operator new(unsigned long) (vg_replace_malloc.c:261)
    ==7749== by 0x4008FE: main (in /home/cc/temp/qsharedptr_test/qsharedptr_test)

    Obviously, QSharedPointer does not delete the original pointer to the address of B as I would expect. Does QSharedPointer not correctly handle multiple inheritance, even for abstract classes?


    Added after 15 minutes:


    By the way, boost::shared_ptr works fine in this case:

    Qt Code:
    1. #include <boost/shared_ptr.hpp>
    2.  
    3. class A
    4. {
    5. int dataA;
    6. public:
    7. virtual void funcA() { }
    8. };
    9.  
    10. class Interface
    11. {
    12. public:
    13. virtual void funcIf()=0;
    14. };
    15.  
    16. class B : public A, public Interface
    17. {
    18. public:
    19. virtual void funcA() { }
    20. virtual void funcIf() { }
    21. };
    22.  
    23. int main()
    24. {
    25. using namespace boost;
    26.  
    27. shared_ptr<B> pB = shared_ptr<B>(new B());
    28. shared_ptr<Interface> pInterface = pB;
    29.  
    30. pB.reset();
    31. pInterface.reset();
    32.  
    33. return 0;
    34. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by chca; 30th January 2011 at 18:28.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSharedPointer does not handle multiple inheritance correctly?

    Don't you need virtual destructor in your case?
    Try this code:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3. #include <iostream>
    4.  
    5. class A
    6. {
    7. int dataA;
    8. public:
    9. virtual void funcA() { }
    10. virtual ~A() { qDebug() << "A d-tor";}
    11. };
    12.  
    13. class Interface
    14. {
    15. public:
    16. virtual void funcIf()=0;
    17. virtual ~Interface() {qDebug() << "Interface d-tor";}
    18. };
    19.  
    20. class B : public A, public Interface
    21. {
    22. public:
    23. virtual void funcA() { }
    24. virtual void funcIf() { }
    25. ~B() {qDebug() << "B d-tor";}
    26. };
    27.  
    28. int main()
    29. {
    30. QSharedPointer<B> pB = QSharedPointer<B>(new B());
    31. QSharedPointer<Interface> pInterface = pB;
    32.  
    33. pB.clear();
    34. pInterface.clear();
    35.  
    36. std::cin.get();
    37. return 0;
    38. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2010
    Posts
    3

    Default Re: QSharedPointer does not handle multiple inheritance correctly?

    Indeed, that solves the issue (yes, I should add a virtual destructor to *every* class). Thanks a lot.

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSharedPointer does not handle multiple inheritance correctly?

    Not all classes should have virtual destructor, only those meant to be inherited.

Similar Threads

  1. Multiple inheritance question
    By marcvanriet in forum Newbie
    Replies: 4
    Last Post: 4th October 2010, 10:30
  2. Multiple inheritance with QObject
    By victor.fernandez in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2010, 16:40
  3. Multiple Inheritance & Qt
    By kefeng.chen in forum Qt Programming
    Replies: 8
    Last Post: 21st March 2006, 18:37
  4. Multiple inheritance & Qt
    By dublet in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 08:12
  5. Multiple Inheritance
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 21st February 2006, 04:00

Tags for this Thread

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.