Skip to content

Commit

Permalink
1) check composer proxy expiration 2) fix a memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
li-feng-sc committed Mar 28, 2024
1 parent 76cc83f commit 1667657
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions support-lib/composer/djinni_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ struct CppProxyCacheEntry {
Composer::Weak<Composer::ValueTypedProxyObject> ref;
int count;
};
class ComposerProxyBase;
extern std::unordered_map<ComposerProxyId, std::weak_ptr<ComposerProxyBase>> jsProxyCache;
extern std::unordered_map<void*, CppProxyCacheEntry> cppProxyCache;
extern std::mutex jsProxyCacheMutex;
extern std::mutex cppProxyCacheMutex;

class ComposerProxyBase {
protected:
Composer::Ref<Composer::ValueTypedProxyObject> _js;
Expand All @@ -473,11 +479,17 @@ class ComposerProxyBase {
public:
ComposerProxyBase(Composer::Ref<Composer::ValueTypedProxyObject> js)
: _js(js), _methods(_js->getTypedObject()->getPropertiesSize()) {}
virtual ~ComposerProxyBase() = default;
virtual ~ComposerProxyBase() {
std::lock_guard lk(jsProxyCacheMutex);
jsProxyCache.erase(_js->getId());
}
Composer::Ref<Composer::ValueTypedProxyObject> getProxy() {
return _js;
}
Composer::Value callJsMethod(size_t i, std::initializer_list<Composer::Value> parameters) {
if (_js->expired()) {
throw JsException(Composer::Error("proxy expired"));
}
if (_methods[i] == nullptr) {
_methods[i] = _js->getTypedObject()->getProperty(i).getFunctionRef();
}
Expand All @@ -492,11 +504,6 @@ class ComposerProxyBase {
}
};

extern std::unordered_map<ComposerProxyId, std::weak_ptr<ComposerProxyBase>> jsProxyCache;
extern std::unordered_map<void*, CppProxyCacheEntry> cppProxyCache;
extern std::mutex jsProxyCacheMutex;
extern std::mutex cppProxyCacheMutex;

template<typename T>
class DjinniCppProxyObject : public Composer::ValueTypedProxyObject {
public:
Expand Down

0 comments on commit 1667657

Please sign in to comment.