未验证 提交 404597c0 编写于 作者: 马根堂 提交者: Gitee

mem leak

Signed-off-by: N马根堂 <magentang4@huawei.com>
上级 74c87f7c
...@@ -64,7 +64,8 @@ private: ...@@ -64,7 +64,8 @@ private:
std::mutex mutex_; std::mutex mutex_;
napi_env env_ = nullptr; napi_env env_ = nullptr;
std::u16string descriptor_; std::u16string descriptor_;
wptr<NAPIRemoteObject> cachedObject_; sptr<NAPIRemoteObject> sptrCachedObject_;
wptr<NAPIRemoteObject> wptrCachedObject_;
napi_ref localInterfaceRef_; napi_ref localInterfaceRef_;
//int32_t attachCount_; //int32_t attachCount_;
}; };
......
...@@ -192,13 +192,6 @@ napi_ref NAPIRemoteObject::GetJsObjectRef() const ...@@ -192,13 +192,6 @@ napi_ref NAPIRemoteObject::GetJsObjectRef() const
{ {
return thisVarRef_; return thisVarRef_;
} }
/*
napi_value NAPIRemoteObject::GetJsObject() const
{
napi_value ret = nullptr;
napi_get_reference_value(env_, thisVarRef_, &ret);
return ret;
}*/
void NAPI_RemoteObject_getCallingInfo(CallingInfo &newCallingInfoParam) void NAPI_RemoteObject_getCallingInfo(CallingInfo &newCallingInfoParam)
{ {
...@@ -594,11 +587,11 @@ napi_value NAPI_ohos_rpc_CreateJsRemoteObject(napi_env env, const sptr<IRemoteOb ...@@ -594,11 +587,11 @@ napi_value NAPI_ohos_rpc_CreateJsRemoteObject(napi_env env, const sptr<IRemoteOb
if (target->CheckObjectLegality()) { if (target->CheckObjectLegality()) {
IPCObjectStub *tmp = static_cast<IPCObjectStub *>(target.GetRefPtr()); IPCObjectStub *tmp = static_cast<IPCObjectStub *>(target.GetRefPtr());
ZLOGI(LOG_LABEL, "[mgttest]object type:%{public}d", tmp->GetObjectType()); uint32_t objectType = tmp->GetObjectType();
if (tmp->GetObjectType() == IPCObjectStub::OBJECT_TYPE_JAVASCRIPT) { ZLOGI(LOG_LABEL, "[mgttest]object type:%{public}d", objectType);
if (objectType == IPCObjectStub::OBJECT_TYPE_JAVASCRIPT || objectType == IPCObjectStub::OBJECT_TYPE_NATIVE) {
//ZLOGI(LOG_LABEL, "napi create js remote object"); //ZLOGI(LOG_LABEL, "napi create js remote object");
sptr<NAPIRemoteObject> object = static_cast<NAPIRemoteObject *>(target.GetRefPtr()); sptr<NAPIRemoteObject> object = static_cast<NAPIRemoteObject *>(target.GetRefPtr());
//return object->GetJsObject();
// retrieve js remote object constructor // retrieve js remote object constructor
napi_value global = nullptr; napi_value global = nullptr;
napi_status status = napi_get_global(env, &global); napi_status status = napi_get_global(env, &global);
...@@ -622,6 +615,7 @@ napi_value NAPI_ohos_rpc_CreateJsRemoteObject(napi_env env, const sptr<IRemoteOb ...@@ -622,6 +615,7 @@ napi_value NAPI_ohos_rpc_CreateJsRemoteObject(napi_env env, const sptr<IRemoteOb
NAPIRemoteObjectHolder *holder = nullptr; NAPIRemoteObjectHolder *holder = nullptr;
napi_unwrap(env, jsRemoteObject, (void **)&holder); napi_unwrap(env, jsRemoteObject, (void **)&holder);
NAPI_ASSERT(env, holder != nullptr, "failed to get napi remote object holder"); NAPI_ASSERT(env, holder != nullptr, "failed to get napi remote object holder");
ZLOGI(LOG_LABEL, "[mgttest]ipc object, save as wptr");
holder->Set(object); holder->Set(object);
return jsRemoteObject; return jsRemoteObject;
} }
......
...@@ -26,7 +26,8 @@ NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16strin ...@@ -26,7 +26,8 @@ NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16strin
NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder() NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder()
{ {
// free the reference of object. // free the reference of object.
cachedObject_ = nullptr; sptrCachedObject_ = nullptr;
wptrCachedObject_ = nullptr;
if (localInterfaceRef_ != nullptr) { if (localInterfaceRef_ != nullptr) {
napi_delete_reference(env_, localInterfaceRef_); napi_delete_reference(env_, localInterfaceRef_);
} }
...@@ -37,10 +38,13 @@ sptr<NAPIRemoteObject> NAPIRemoteObjectHolder::Get(napi_value jsRemoteObject) ...@@ -37,10 +38,13 @@ sptr<NAPIRemoteObject> NAPIRemoteObjectHolder::Get(napi_value jsRemoteObject)
std::lock_guard<std::mutex> lockGuard(mutex_); std::lock_guard<std::mutex> lockGuard(mutex_);
// grab an strong reference to the object, // grab an strong reference to the object,
// so it will not be freed util this reference released. // so it will not be freed util this reference released.
sptr<NAPIRemoteObject> tmp = cachedObject_.promote(); if (sptrCachedObject_ != nullptr) {
return sptrCachedObject_;
}
sptr<NAPIRemoteObject> tmp = wptrCachedObject_.promote();
if (tmp == nullptr) { if (tmp == nullptr) {
tmp = new NAPIRemoteObject(env_, jsRemoteObject, descriptor_); tmp = new NAPIRemoteObject(env_, jsRemoteObject, descriptor_);
cachedObject_ = tmp; wptrCachedObject_ = tmp;
} }
return tmp; return tmp;
} }
...@@ -48,7 +52,12 @@ sptr<NAPIRemoteObject> NAPIRemoteObjectHolder::Get(napi_value jsRemoteObject) ...@@ -48,7 +52,12 @@ sptr<NAPIRemoteObject> NAPIRemoteObjectHolder::Get(napi_value jsRemoteObject)
void NAPIRemoteObjectHolder::Set(sptr<NAPIRemoteObject> object) void NAPIRemoteObjectHolder::Set(sptr<NAPIRemoteObject> object)
{ {
std::lock_guard<std::mutex> lockGuard(mutex_); std::lock_guard<std::mutex> lockGuard(mutex_);
cachedObject_ = object; IPCObjectStub *tmp = static_cast<IPCObjectStub *>(object.GetRefPtr());
if (tmp->GetObjectType() == IPCObjectStub::OBJECT_TYPE_JAVASCRIPT) {
wptrCachedObject_ = object;
} else {
sptrCachedObject_ = object;
}
} }
void NAPIRemoteObjectHolder::attachLocalInterface(napi_value localInterface, std::string &descriptor) void NAPIRemoteObjectHolder::attachLocalInterface(napi_value localInterface, std::string &descriptor)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册