Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QSharedData/QSharedDataPointer: questions on Qt-example

[Solved] QSharedData/QSharedDataPointer: questions on Qt-example

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thEClaw
    wrote on last edited by
    #1

    I just read the example for "QSharedDataPointer":http://qt-project.org/doc/qt-5/qshareddatapointer.html (it's right there in the documentation). And there are a couple of details I didn't immediately understand - maybe somebody can help. I will just copy the example and add some comments:

    @#include <QSharedData>
    #include <QString>

    class EmployeeData : public QSharedData
    {
    public:
    EmployeeData() : id(-1) { } //Why is the QSharedData-constructor not called?
    EmployeeData(const EmployeeData &other)
    : QSharedData(other), id(other.id), name(other.name) { }
    ~EmployeeData() { }

    int id;
    QString name;
    

    };

    class Employee
    {
    public:
    Employee() { d = new EmployeeData; } //memory for d is dynamically allocated but never freed?
    Employee(int id, QString name) {
    d = new EmployeeData;
    setId(id);
    setName(name);
    }
    Employee(const Employee &other)
    : d (other.d)
    {
    }
    void setId(int id) { d->id = id; }
    void setName(QString name) { d->name = name; }

    int id() const { return d->id; }
    QString name() const { return d->name; }
    

    private:
    QSharedDataPointer<EmployeeData> d;
    };@

    Especially the point of d using dynamically allocated memory is bugging me. I could imagine that "QSharedData":http://qt-project.org/doc/qt-5/qshareddata.html deletes itself when appropriate but I didn't find anything about that in the documentation. Hence this looks like a memory leak to me.

    I hope somebody can help me out. :)

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Possible. Just call destructor on QSharedDataPointer. It should release. You can also check with creating few thousand variables and releasing them. See how it goes.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        d is a not a pointer it's in a "container" class that stores the pointer to the allocated QSharedData derived class. So when the Employee object is destroyed, the data will be automatically released if there's no more reference to it.

        @Dheerendra don't call destructors directly, they are not meant to be used like that.

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thEClaw
          wrote on last edited by
          #4

          Thanks a lot. :)

          Do you happen to know anything about the second "problem", too?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Do you mean the constructor ?

            Sure it's called, why do you think it's not ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thEClaw
              wrote on last edited by
              #6

              Because it is not called? Or did I miss some feature of C++ where baseclass constructors are automatically called?

              EDIT: Just to clarify things:
              @class EmployeeData : public QSharedData
              {
              public:
              EmployeeData() : id(-1) { }@
              This is an inline method, a constructor that does not call QSharedData() - maybe it's a bug in the documentation, but I wanted to be sure.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                AFAIK, base class constructors are automatically called for you if they have no argument(s).

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  thEClaw
                  wrote on last edited by
                  #8

                  Oh, thank you. That is something I didn't know yet.

                  I will mark the thread as solved, then. :) (Thanks again!)

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved