Memory management with QObjects



  • 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?



  • 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.


  • Moderators

    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.



  • 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 )
    {
    ..........
    }



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.