Results 1 to 5 of 5

Thread: 100% correct usage of QSharedData

  1. #1

    Default 100% correct usage of QSharedData

    Hi all,

    Just tried to create my first data class using Qt's implicit sharing, but the documentation out there is misleading in multiple cases.
    My header currently looks like this:
    Qt Code:
    1. class MyData;
    2.  
    3. class MyBase
    4. {
    5. public:
    6. MyBase(const QString& name, const QString& address);
    7.  
    8. const QString& getName() const;
    9. const QString& getAddress() const;
    10.  
    11. private:
    12. QSharedDataPointer<MyData> d;
    13. };
    14.  
    15. class MyData : public QSharedData
    16. {
    17. public:
    18. QString name;
    19. QString address;
    20. };
    To copy to clipboard, switch view to plain text mode 
    Source file:
    Qt Code:
    1. MyBase::MyBase(const QString& name, const QString& address)
    2. {
    3. d = new MyData();
    4. d->name = name;
    5. d->address = address;
    6. }
    7.  
    8. const QString& getName() const
    9. {
    10. return d->name;
    11. }
    12.  
    13. const QString& getAddress() const
    14. {
    15. return d->address;
    16. }
    To copy to clipboard, switch view to plain text mode 

    This already compiles, but misses some parts depending on what documentation you read. What comes to mind:
    1. Empty destructor ~MyBase() {}
    2. Copy constructor MyBase(const MyBase& other) : d(other.d) {}
    3. Assignment operator operator=(const MyBase& other) {if(this != &other) d = other.d; return *this;}
    4. Constructors, destructors and assignment operators in MyData as well


    Is any of them strictly required or does my implementation also work well without them? Maybe some are implicitly compiler-generated, I have no idea.
    I've read that this also depends on whether you declare MyData inside the MyBase header file. I definitely want to do this, so please tell me if this is the reason I can leave out these things.

    Thank you!

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 100% correct usage of QSharedData

    mydata doesn't need anything added. mybase needs a dtor otherwise you have memory leak.
    Last edited by amleto; 20th May 2012 at 01:52.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: 100% correct usage of QSharedData

    The declaration and implementation of the private class MyData would typically both be in the cpp file. This hides the internal structures from those that do not need to know about them. The header file declares MyBase and only provides a forward declaration of MyData in order to make a pointer to one.

  4. #4

    Default Re: 100% correct usage of QSharedData

    Quote Originally Posted by amleto View Post
    mybase needs a dtor otherwise you have memory leak.
    How does an empty destructor prevent a memory leak?

    Quote Originally Posted by ChrisW67 View Post
    The declaration and implementation of the private class MyData would typically both be in the cpp file. This hides the internal structures from those that do not need to know about them.
    As I'm developing a standalone application, which isn't mixed with code I don't know about, I don't care who can see the internal structures.

    But can anyone confirm that I'm not missing any constructors or overloaded operators for a functioning shared container class?
    If not, what are these additional copy constructors or assignment operators for?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: 100% correct usage of QSharedData

    Quote Originally Posted by coder2012 View Post
    How does an empty destructor prevent a memory leak?
    It doesn't (at least a non-virtual destructor). Destruction must deallocate resources allocated during construction. If you were not using the smart pointer then the allocated MyData instance would be a memory leak requiring a non-empty destructor. In this instance the QSharedDataPointer will delete the MyData instance if necessary, and the d object will always be destroyed with your MyBase object.

    If you intend that MyBase can be inherited then it should have an empty virtual destructor so that memory is not leaked by deleting a derived object through a MyBase*.

    As I'm developing a standalone application, which isn't mixed with code I don't know about, I don't care who can see the internal structures.
    Fine. Your call.

    But can anyone confirm that I'm not missing any constructors or overloaded operators for a functioning shared container class?
    If not, what are these additional copy constructors or assignment operators for?
    You need a copy constructor and assignment operator if the default C++ behaviour is improper or if you wish to prohibit these actions by making them private. QSharedDataPointer, through its copy constructor/assignment operator, takes care of a great deal for you during a default copy, so you more than likely do not need a copy constructor. Disabling copy in a shared data class does not immediately strike me as sensible; you could not share the data.

Similar Threads

  1. Correct usage of QThread
    By Patrik in forum Qt Programming
    Replies: 6
    Last Post: 8th February 2012, 15:54
  2. problem with QString and QsharedData
    By jrch2k10 in forum Qt Programming
    Replies: 4
    Last Post: 12th July 2011, 14:45
  3. Replies: 1
    Last Post: 9th May 2011, 13:16
  4. QsharedMemory or QsharedData ?
    By OverTheOCean in forum Qt Programming
    Replies: 2
    Last Post: 7th November 2010, 22:33
  5. QSharedData - implicit sharing
    By gyre in forum Newbie
    Replies: 4
    Last Post: 28th October 2007, 19:09

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.