Deleting objects with mixed ScriptOwnership



  • I'm running into a situation that is causing an exception when I delete an object that is not owned by the script engine however the object that owns the object does have script ownership. I'll try to explain...

    I have an object that lives in the global script space, lets call it "Factory" and it has a method "getItem(name)". getItem will then create a two objects, one is an object that gets returned to the script, lets call this ScriptObj and another object called PrivateObj which ScriptObj has a pointer to. ScriptObj gets ScriptOwnership and PrivateObj does not get exposed to the scripting engine.

    @
    class ScriptObj : public QObject
    {
    ScriptObj(PrivateObj * po, QObject * parent = 0) : QObject(parent)
    {
    _po = po;
    }

    virtual ~ScriptObj()
    {
    delete _po;
    }

    private PrivateObj * _po;
    };

    QObject * Factory::getItem(name)
    {
    PrivateObj * po = new PrivateObj();
    ScriptObj * sc = new ScriptObj(po);
    _scriptEngine->newQObject(sc, QScriptEngine::ScriptOwnership);
    return sc;
    }
    @

    Next I start a script that calls ino getItem, does some processing.
    The script finishes and I proceed to delete the scripting engine.

    @
    _scriptEngine = new QScriptEngine();
    QScriptValue v = _scriptEngine->newQObject(_factory, QScriptEngine::QtOwnership);
    _scriptEngine->globalObject().setProperty("Factory", v);

    _scriptEngine->evaluate("var item = Factory.getItem("someItem"); item.doSomething();");

    delete _scriptEngine;
    @

    When _scriptEngine gets deleted I get a debug assertion: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) in the ScriptObj desctructor when it trys to delete _po;

    I have verified that this is happens on the first call (ie., that its not being called multiple times) and I have also verified that I am deleting the same address that PrivateObj was created with. I'm not quite sure what I am doing wrong.

    I appreciate any help.

    Thanks.


Log in to reply
 

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