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. Delete or not?[RESOLVED]
Forum Updated to NodeBB v4.3 + New Features

Delete or not?[RESOLVED]

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.7k 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.
  • G Offline
    G Offline
    GrahamL
    wrote on last edited by
    #1

    Hi
    I am unsure as to when I need to use delete
    given this code
    @
    namespace Reporting
    {
    class ReportElementWriter: public QObject
    {

    Q_OBJECT

    public:

    ReportElementWriter(const QString& filename,QObject* parent = 0);
    virtual ~ReportElementWriter();
    bool write(const QTextDocument* document);

    private:
    QIODevice m_QIODevice;
    QIODevice
    m_contentStream;
    QuaZip *m_zipArchive;
    QBuffer m_contentBuffer;
    };

    }
    @
    In the corresponding implentation file I allocate the member variables
    @
    m_QIODevice(new QFile(filename)
    @

    Do I need to delete them in my destructor?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Yes, you do, if you use this way of creating a new QFile object.

      QObjects and subclasses there of (that includes QFile) all have constructors that optionally take a parent object. If you use that constructor to set a parent, that parent becomes responsible for the life time of the object you created. If you don't use it, it is your responsibility to otherwise make sure you delete the object instance when you're done with it.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GrahamL
        wrote on last edited by
        #3

        Hi thanks for clarifying that for me
        So if I create the QFile object like this
        @
        m_QIODevice(new QFile(filename,parent)
        @
        I would not need to delete m_QIODevice?

        THnaks

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          That's correct. You still could, but if not, the parent object will delete it (m_QIODevice) when it (the parent) gets deleted.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            GrahamL
            wrote on last edited by
            #5

            Thanks

            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