From 1667657096bd0d62d6b4bf978456672cd441c666 Mon Sep 17 00:00:00 2001 From: Li Feng Date: Thu, 28 Mar 2024 16:23:01 +1100 Subject: [PATCH] 1) check composer proxy expiration 2) fix a memory leak --- support-lib/composer/djinni_composer.hpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/support-lib/composer/djinni_composer.hpp b/support-lib/composer/djinni_composer.hpp index a716590b..707af64b 100644 --- a/support-lib/composer/djinni_composer.hpp +++ b/support-lib/composer/djinni_composer.hpp @@ -465,6 +465,12 @@ struct CppProxyCacheEntry { Composer::Weak ref; int count; }; +class ComposerProxyBase; +extern std::unordered_map> jsProxyCache; +extern std::unordered_map cppProxyCache; +extern std::mutex jsProxyCacheMutex; +extern std::mutex cppProxyCacheMutex; + class ComposerProxyBase { protected: Composer::Ref _js; @@ -473,11 +479,17 @@ class ComposerProxyBase { public: ComposerProxyBase(Composer::Ref js) : _js(js), _methods(_js->getTypedObject()->getPropertiesSize()) {} - virtual ~ComposerProxyBase() = default; + virtual ~ComposerProxyBase() { + std::lock_guard lk(jsProxyCacheMutex); + jsProxyCache.erase(_js->getId()); + } Composer::Ref getProxy() { return _js; } Composer::Value callJsMethod(size_t i, std::initializer_list parameters) { + if (_js->expired()) { + throw JsException(Composer::Error("proxy expired")); + } if (_methods[i] == nullptr) { _methods[i] = _js->getTypedObject()->getProperty(i).getFunctionRef(); } @@ -492,11 +504,6 @@ class ComposerProxyBase { } }; -extern std::unordered_map> jsProxyCache; -extern std::unordered_map cppProxyCache; -extern std::mutex jsProxyCacheMutex; -extern std::mutex cppProxyCacheMutex; - template class DjinniCppProxyObject : public Composer::ValueTypedProxyObject { public: