Results 1 to 2 of 2

Thread: QtScript and c++ object lifetime: create in script, using in c++ code

  1. #1
    Join Date
    Oct 2009
    Location
    Russia, South Ural, Chelyabinsk
    Posts
    42
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QtScript and c++ object lifetime: create in script, using in c++ code

    Hi!
    In my application I need to create object in script (var x = new MyWrapObject()) and then use it at c++ code

    Have I any problems with memory leak or maybe script garbage collector delete object then I use it in c++ code

    Qt help:
    Controlling QObject Ownership

    By default, the script engine does not take ownership of the QObject that is passed to QScriptEngine::newQObject(); the object is managed according to Qt's object ownership (see Object Trees and Object Ownership). You can change this behavior by specifying a different ownership mode as the second argument, overriding the default (QScriptEngine::QtOwnership).

    Specifying QScriptEngine::ScriptOwnership as the ownership mode will cause the script engine to take full ownership of the QObject and delete it when it determines that it is safe to do so. This ownership mode is appropriate if the QObject does not have a parent object, and/or the QObject is created in the context of the script engine and is not intended to outlive the script engine.

    For example, a constructor function that constructs QObjects only to be used in the script environment is a good candidate:
    QScriptValue myQObjectConstructor(QScriptContext *context, QScriptEngine *engine)
    {
    // let the engine manage the new object's lifetime.
    return engine->newQObject(new MyQObject(), QScriptEngine::ScriptOwnership);
    }

    Another ownership mode is QScriptEngine::AutoOwnership, where the ownership is based on whether the QObject has a parent or not. If the QtScript garbage collector finds that the QObject is no longer referenced within the script environment, the QObject will be deleted only if it does not have a parent.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript and c++ object lifetime: create in script, using in c++ code

    Just call a special c++ function from script, that creates the object. Thats what the constructor function is anyway, when you have declared your own.

    There, when you return the qobjects script value back using engine->newQObject(), just pass QtOwnership as parameter. Then script garbage collection will not delete your object.

    If you need to use it in c++ code, you will have to find some way of accessing it. Just store a pointer/pointers to your object(s) when you create them.

    HIH

    Johannes

Similar Threads

  1. Replies: 4
    Last Post: 11th May 2010, 17:33
  2. Replies: 1
    Last Post: 19th February 2010, 11:02
  3. QtScript. How to add created object to script?
    By sergey_85 in forum Qt Programming
    Replies: 1
    Last Post: 18th February 2010, 14:24
  4. QtScript: how to reload current script?
    By eyeofhell in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2010, 10:28
  5. [Advanced] Qt Script access QObject from C++ code
    By bunjee in forum Qt Programming
    Replies: 5
    Last Post: 21st May 2009, 22:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.