Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 3 files). Show all
Unified · Split
| @@ -274,7 +274,17 @@ static void resultHandlerFromScriptValue(const QScriptValue& value, AnimVariantR · 273 unmodified lines | |||
| 274 | using ScriptableResourceRawPtr = ScriptableResource*; | 274 | using ScriptableResourceRawPtr = ScriptableResource*; |
| 275 | 275 | ||
| 276 | static QScriptValue scriptableResourceToScriptValue(QScriptEngine* engine, const ScriptableResourceRawPtr& resource) { | 276 | static QScriptValue scriptableResourceToScriptValue(QScriptEngine* engine, const ScriptableResourceRawPtr& resource) { |
| 277 | resource->updateMemoryCost(engine); | 277 | // The first script to encounter this resource will track its memory. |
| 278 | // In this way, it will be more likely to GC. | ||
| 279 | // This fails in the case that the resource is used across many scripts, but | ||
| 280 | // in that case it would be too difficult to tell which one should track the memory, and | ||
| 281 | // this serves the common case (use in a single script). | ||
| 282 | auto data = resource->getResource(); | ||
| 283 | if (data && !resource->isInScript()) { | ||
| 284 | resource->setInScript(true); | ||
| 285 | QObject::connect(data.data(), SIGNAL(updateSize(qint64)), engine, SLOT(updateMemoryCost(qint64))); | ||
| 286 | } | ||
| 287 | |||
| 278 | auto object = engine->newQObject( | 288 | auto object = engine->newQObject( |
| 279 | const_cast<ScriptableResourceRawPtr>(resource), | 289 | const_cast<ScriptableResourceRawPtr>(resource), |
| 280 | QScriptEngine::ScriptOwnership); | 290 | QScriptEngine::ScriptOwnership); |