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. Memory management with QObjects
Forum Updated to NodeBB v4.3 + New Features

Memory management with QObjects

Scheduled Pinned Locked Moved General and Desktop
qobjectpointermemoryinheritance
4 Posts 4 Posters 2.1k Views 3 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.
  • R Offline
    R Offline
    RDiGuida
    wrote on 20 Apr 2015, 14:06 last edited by
    #1

    Hi, I am trying to initialise an object derived from a QObject. The class definition is as follows.

    #define CONTACT_H
    #include <QObject>
    #include <QString>
    
    class Contact : public QObject
    {
    public:
        Contact(int cat=0,QString fir="",QString lst="",QString ad="",QString zp="",
                QString ct="",QString nm="",QObject *parent=0);
        ~Contact();
        void SetContList(QObject* par);
        void DestroyCont();
        QString toString();
    
    private:
        int categ;
        QString first;
        QString last;
        QString Add;
        QString zip;
        QString city;
        QString num;
    
        QObject *Parent;
    };
    
    #endif // CONTACT_H
    

    with the following implementation

    #include <QTextStream>
    
    Contact::Contact(int cat,QString fir,QString lst,QString ad,QString zp,
                     QString ct,QString nm,QObject *parent)
    {
        categ=cat;
        first=fir;
        last=lst;
        Add=ad;
        zip=zp;
        city=ct;
        num=nm;
    
        Parent = new QObject(parent);
    
        setObjectName(ad);
    }
    
    void Contact::SetContList(QObject* par)
    {
        this->setParent(par);
    }
    
    void Contact::DestroyCont()
    {
        delete this;
    }
    
    Contact::~Contact()
    {
        DestroyCont();
    }
    
    QString Contact::toString()
    {
        QString fin;
        QTextStream out(&fin);
        out << categ << "\t" << first << "\t" << last << "\t" << Add << '\t'
            << zip << "\t" << city << "\t" << num;
    
        return fin;
    }
    

    I have been trying to initialise an Object of class Contact in the main but the program crashes.

    #include <contactlist.h>
    
    int main(int argc, char *argv[])
    {
        Contact c1(1,"John","Smith","Smith Road","B29 6BS","Birmingham","023415");
        return 0;
    }
    

    Contact list is another class where contact.h is invoked. Conversely, if I change the main line into

    Contact *c1 = new Contact(1,"John","Smith","Smith Road","B29 6BS","Birmingham","023415");
    

    The program doesn't crash. Why is that?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sneubert
      wrote on 20 Apr 2015, 14:15 last edited by
      #2

      In your destructor you call delete on this. If you create a local object without the newoperator your object
      gets created on the stack. So for writing Contact c1the deletecall will crash your program, as for every deleteyou need a new.

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 20 Apr 2015, 14:48 last edited by Chris Kawa
        #3

        delete this is called in the function called from the destructor... which is called when the object is deleted (or goes out of scope) so that's a little bit of recursive delete. Don't do that.

        Apart from that creating QObjects requires an existing QApplication instance so you need to create it in the main before you create your object.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alex_malyu
          wrote on 21 Apr 2015, 01:16 last edited by
          #4

          If your question was how to initialize QObject from child
          you do not need extra Parent variable,
          no extra deletes, just add it in constructor:

          Contact::Contact(int cat,QString fir,QString lst,QString ad,QString zp,
          QString ct,QString nm,QObject *parent)
          ::QObject( parent )
          {
          ..........
          }

          1 Reply Last reply
          0

          1/4

          20 Apr 2015, 14:06

          • Login

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