-
 KDE-Apps.org Applications for the KDE-Desktop 
 GTK-Apps.org Applications using the GTK Toolkit 
 GnomeFiles.org Applications for GNOME 
 MeeGo-Central.org Applications for MeeGo 
 CLI-Apps.org Command Line Applications 
 Qt-Apps.org Free Qt Applications 
 Qt-Prop.org Proprietary Qt Applications 
 Maemo-Apps.org Applications for the Maemo Plattform 
 Java-Apps.org Free Java Applications 
 eyeOS-Apps.org Free eyeOS Applications 
 Wine-Apps.org Wine Applications 
 Server-Apps.org Server Applications 
 apps.ownCloud.com ownCloud Applications 
--
-
 KDE-Look.org Artwork for the KDE-Desktop 
 GNOME-Look.org Artwork for the GNOME-Desktop 
 Xfce-Look.org Artwork for the Xfce-Desktop 
 Box-Look.org Artwork for your Windowmanager 
 E17-Stuff.org Artwork for Enlightenment 
 Beryl-Themes.org Artwork for the Beryl Windowmanager 
 Compiz-Themes.org Artwork for the Compiz Windowmanager 
 EDE-Look.org Themes for your EDE Desktop 
--
-
 Debian-Art.org Stuff for Debian 
 Gentoo-Art.org Artwork for Gentoo Linux 
 SUSE-Art.org Artwork for openSUSE 
 Ubuntu-Art.org Artwork for Ubuntu 
 Kubuntu-Art.org Artwork for Kubuntu 
 LinuxMint-Art.org Artwork for Linux Mint 
 Arch-Stuff.org Art And Stuff for Arch Linux 
 Frugalware-Art.org Themes for Frugalware 
 Fedora-Art.org Artwork for Fedora Linux 
 Mandriva-Art.org Artwork for Mandriva Linux 
--
-
 KDE-Files.org Files for KDE Applications 
 OpenTemplate.org Documents for OpenOffice.org
 GIMPStuff.org Files for GIMP
 InkscapeStuff.org Files for Inkscape
 ScribusStuff.org Files for Scribus
 BlenderStuff.org Textures and Objects for Blender
 VLC-Addons.org Themes and Extensions for VLC
--
-
 KDE-Help.org Support for your KDE Desktop 
 GNOME-Help.org Support for your GNOME Desktop 
 Xfce-Help.org Support for your Xfce Desktop 
--
openDesktop.orgopenDesktop.org:   Applications   Artwork   Linux Distributions   Documents    Linux42.org    OpenSkillz.com   
 
Home
Apps
Artwork
News
Groups
Knowledge
Events
Forum
People
Jobs
Register
Login

-
- News . 
0
votes
click to vote up

Aurélien Gâteau: Any Object Left Without a Parent Will Be Destroyed


Published Sep 15 2015 via RSS

Recently we had to fix a tricky crash at work: our Qt Quick app was crashing randomly when switching between two pages of the app.

The backtrace looked as if a QObject::deleteLater was trying to delete an already deleted object. Running the app with Valgrind confirmed we were trying to delete an already deleted pointer.

After much debugging, we found out the reason for the crash. Our app contains a C++ Qt model, which wraps a list of QObject. Some properties of the QObject are exposed as roles in the model, but we also have a getter which returns the QObject itself.

Here is a rough summary of the code.

The object used for the rows of the model:

class MyObject: public QObject {
    Q_OBJECT
public:
    MyObject(int value, QObject* parent = 0);
    Q_INVOKABLE doSomething();
};

The model looks like this:

class MyModel : public QAbstractListModel {
    Q_OBJECT
public:
    MyModel(QObject* parent = 0)
    : QAbstractListModel(parent) {
        mObjects
            << new MyObject(12)
            << new MyObject(34);
    }

    ~MyModel() {
        qDeleteAll(mObjects);
    }

    Q_INVOKABLE MyObject* objectAt(int row) const {
        return mObjects[row];
    }

private:
    QList<MyObject*> mObjects;
};

The QML code uses the model like this:

ListView {
    model: MyModel {
        id: myModel
    }

    delegate: Row {
        Text {
            text: model.value
        }
        Button {
            text: "Do something"
            onClicked: {
                var myObject = myModel.objectAt(model.index);
                myObject.doSomething();
            }
        }
    }

Can you spot the bug?

Here is what's wrong: when QML gets a MyObject instance through the objectAt() method, it receives a QObject, but this QObject has no parent (since MyModel constructor creates the MyObject instances without setting a parent). What does QML do if it gets a parent-less QObject? The answer is in QQmlEngine::ObjectOwnership doc: it takes ownership of it... and will garbage collect it when it feels like doing some clean up. If QML garbage collector runs before MyModel destructor, the destructor will crash in the qDeleteAll() call...

We fixed it by making the MyObject instances children of the model. Since the objects now have a parent, QML does not take ownership of them anymore. We assumed it was not necessary because we were taking care of deleting all instances with the qDeleteAll() in the destructor, but in this case, that turned out to be an error. Another possible approach (which we did not try) would have been to explicitly set the ownership using QQmlEngine::setOwnership().

So remember, do not leave any QObject unattended without a parent, otherwise the QML engine will destroy it!



BackRead original postSend to a friend

Add comment Add comment
Show all posts




-



 
 
 Who we are
Contact
More about us
Frequently Asked Questions
Register
Twitter
Blog
Explore
Apps
Artwork
Jobs
Knowledge
Events
People
Updates on identi.ca
Updates on Twitter
Content RSS   
Events RSS   

Participate
Groups
Forum
Add Content
Public API
About openDesktop.org
Legal Notice
Spreadshirt Shop
CafePress Shop
Advertising
Sponsor us
Report Abuse
 

Copyright 2007-2016 openDesktop.org Team  
All rights reserved. openDesktop.org is not liable for any content or goods on this site.
All contributors are responsible for the lawfulness of their uploads.
openDesktop is a trademark of the openDesktop.org Team