From 3636e666528b72de79f8c7012690bb9e279f0863 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 24 Aug 2009 18:57:06 +0200 Subject: Fix memory leak in QScriptEngine::newQObject Do not reset the delegate and the prototype when reusing existing wrapper. Reviewed-by: Kent Hansen --- src/script/api/qscriptengine.cpp | 14 ++++++-------- src/script/bridge/qscriptobject.cpp | 2 ++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index 631175a..033d92e 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -1180,14 +1180,14 @@ JSC::JSValue QScriptEnginePrivate::newQObject( bool preferExisting = (options & QScriptEngine::PreferExistingWrapperObject) != 0; QScriptEngine::QObjectWrapOptions opt = options & ~QScriptEngine::PreferExistingWrapperObject; QScriptObject *result = 0; - if (preferExisting) + if (preferExisting) { result = data->findWrapper(ownership, opt); - if (!result) { - result = new (exec) QScriptObject(qobjectWrapperObjectStructure); - if (preferExisting) - data->registerWrapper(result, ownership, opt); + if (result) + return result; } - Q_ASSERT(result != 0); + result = new (exec) QScriptObject(qobjectWrapperObjectStructure); + if (preferExisting) + data->registerWrapper(result, ownership, opt); result->setDelegate(new QScript::QObjectDelegate(object, ownership, options)); /*if (setDefaultPrototype)*/ { const QMetaObject *meta = object->metaObject(); @@ -1635,7 +1635,6 @@ QScriptValue QScriptEngine::newVariant(const QScriptValue &object, } QScriptObject *jscScriptObject = static_cast(jscObject); if (!object.isVariant()) { - delete jscScriptObject->delegate(); jscScriptObject->setDelegate(new QScript::QVariantDelegate(value)); } else { QScriptValuePrivate::get(object)->setVariantValue(value); @@ -1713,7 +1712,6 @@ QScriptValue QScriptEngine::newQObject(const QScriptValue &scriptObject, } QScriptObject *jscScriptObject = static_cast(jscObject); if (!scriptObject.isQObject()) { - delete jscScriptObject->delegate(); jscScriptObject->setDelegate(new QScript::QObjectDelegate(qtObject, ownership, options)); } else { QScript::QObjectDelegate *delegate = static_cast(jscScriptObject->delegate()); diff --git a/src/script/bridge/qscriptobject.cpp b/src/script/bridge/qscriptobject.cpp index 2022baf..e59c542 100644 --- a/src/script/bridge/qscriptobject.cpp +++ b/src/script/bridge/qscriptobject.cpp @@ -95,6 +95,8 @@ void QScriptObject::setDelegate(QScriptObjectDelegate *delegate) { if (!d) d = new Data(); + else + delete d->delegate; d->delegate = delegate; } -- cgit v0.12