Results 1 to 4 of 4

Thread: QExplicitlySharedDataPointer and circular references lead to dangling pointers

  1. #1
    Join Date
    Dec 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QExplicitlySharedDataPointer and circular references lead to dangling pointers

    Hi,

    I want my classes to use QExplicitlySharedDataPointer for their shared data. Also I want several classes to reference each other.

    We have ClassX and ClassY. ClassX maintains a QList<ClassY>. ClassY references exactly one ClassX. Both classes implement copy constructor and operator=(), like this:

    Qt Code:
    1. ClassY::ClassY(const ClassY &other) :
    2. QObject(other.parent()),
    3. d( other.d )
    4. {
    5. }
    6.  
    7. ClassY &ClassY::operator =(const ClassY &other)
    8. {
    9. d = other.d;
    10. return *this;
    11. }
    To copy to clipboard, switch view to plain text mode 

    When we now use the following code to add/set the values, it leads to circular dependencies between the two classes:

    Qt Code:
    1. void ClassY::setClassX(const ClassX &classX)
    2. {
    3. d->m_classX = classX;
    4. }
    5.  
    6. void ClassX::addClassY(const ClassY &classY)
    7. {
    8. d->m_classYList.append(classY);
    9. }
    To copy to clipboard, switch view to plain text mode 

    When we execute the following test, the shared data objects are never being destroyed and leak:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. ClassX x1;
    6. ClassY y1;
    7.  
    8. x1.addClassY(y1);
    9. y1.setClassX(x1);
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    I create the output with the help of incremental IDs for each instance of ClassX, ClassY, ClassXPrivate and ClassYPrivate:

    Qt Code:
    1. ClassXPrivate 0
    2. ClassX 0
    3. ClassXPrivate 1
    4. ClassX 1
    5. ClassYPrivate 0
    6. ClassY 0
    7. ~ClassXPrivate 1
    8. ~ClassY 0
    9. ~ClassX 0
    To copy to clipboard, switch view to plain text mode 

    As you can see, ClassYPrivate 1 and ClassXPrivate 0 are never being destructed.


    You may download the small example Qt Creator project from my dropbox: http://dl.dropbox.com/u/140012/sharedatatest.zip


    I came across this behavious in the libtvdb library, in which each Series has several Seasons, which has several Episodes. Each Season knows its Series and each Episode knows its Season and Series. This leads to HUGE memory leaks, if you scrape several hundred TV shows.

    Am I right, with what I am saying? How would you solve this problem?


    Greetings Niklas

  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: QExplicitlySharedDataPointer and circular references lead to dangling pointers

    IMHO one reference should be a weak reference, then there won't be any problems.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QExplicitlySharedDataPointer and circular references lead to dangling pointers

    What do you mean by "weak" reference? Both "references" are simple members on the stack:

    Qt Code:
    1. ClassXPrivate {
    2. ...
    3. QList<ClassY> m_classYList;
    4. ...
    5. }
    6. ClassYPrivate {
    7. ...
    8. ClassX m_classX;
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 


    Since all objects are quite temporary, I don't want them on my heap. Thats why I don't want to reference them with the help of pointers. I just want to use them, just like we all do with QString etc.

  4. #4
    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: QExplicitlySharedDataPointer and circular references lead to dangling pointers

    A weak reference is one that only checks existance of the object but does not enforce that existance (in other words does not "own" the object). See the difference between QSharedPointer and QWeakPointer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Inheritance and QExplicitlySharedDataPointer
    By tbcpp in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2010, 08:18
  2. QExplicitlySharedDataPointer
    By rhf417 in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2010, 21:51
  3. QExplicitlySharedDataPointer
    By qtuser20 in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2009, 07:05
  4. Pointers and references
    By PaceyIV in forum Newbie
    Replies: 10
    Last Post: 27th July 2009, 10:53
  5. Checking if a pointer is dangling
    By Cruz in forum Qt Programming
    Replies: 8
    Last Post: 15th February 2009, 13:13

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.