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

mem leak

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