Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
communication_ipc
提交
ce876990
C
communication_ipc
项目概览
OpenHarmony
/
communication_ipc
大约 1 年 前同步成功
通知
20
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
communication_ipc
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ce876990
编写于
6月 19, 2023
作者:
马
马根堂
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
NAPIRemoteObject内存泄露修改
Signed-off-by:
N
马根堂
<
magentang4@huawei.com
>
上级
4a82acc0
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
42 addition
and
38 deletion
+42
-38
ipc/native/src/core/source/ipc_object_proxy.cpp
ipc/native/src/core/source/ipc_object_proxy.cpp
+3
-0
ipc/native/src/core/source/ipc_object_stub.cpp
ipc/native/src/core/source/ipc_object_stub.cpp
+2
-2
ipc/native/src/napi_common/include/napi_remote_object_holder.h
...ative/src/napi_common/include/napi_remote_object_holder.h
+6
-5
ipc/native/src/napi_common/include/napi_remote_object_internal.h
...ive/src/napi_common/include/napi_remote_object_internal.h
+2
-1
ipc/native/src/napi_common/source/napi_remote_object.cpp
ipc/native/src/napi_common/source/napi_remote_object.cpp
+16
-24
ipc/native/src/napi_common/source/napi_remote_object_holder.cpp
...tive/src/napi_common/source/napi_remote_object_holder.cpp
+13
-6
未找到文件。
ipc/native/src/core/source/ipc_object_proxy.cpp
浏览文件 @
ce876990
...
...
@@ -530,6 +530,9 @@ int IPCObjectProxy::GetProtoInfo()
switch
(
reply
.
ReadUint32
())
{
case
IRemoteObject
::
IF_PROT_BINDER
:
{
remoteDescriptor_
=
reply
.
ReadString16
();
ZLOGD
(
LABEL
,
"it is normal binder, handle:%{public}u desc:%{public}s"
,
handle_
,
Str16ToStr8
(
remoteDescriptor_
).
c_str
());
break
;
}
case
IRemoteObject
::
IF_PROT_DATABUS
:
{
...
...
ipc/native/src/core/source/ipc_object_stub.cpp
浏览文件 @
ce876990
...
...
@@ -371,8 +371,8 @@ int IPCObjectStub::GetObjectType() const
int32_t
IPCObjectStub
::
ProcessProto
(
uint32_t
code
,
MessageParcel
&
data
,
MessageParcel
&
reply
,
MessageOption
&
option
)
{
int
result
=
ERR_NONE
;
ZLOGD
(
LABEL
,
"IPCObjectStub::ProcessProto called, type = 0, normal stub object
"
);
if
(
!
reply
.
WriteUint32
(
IRemoteObject
::
IF_PROT_BINDER
))
{
ZLOGD
(
LABEL
,
"IPCObjectStub::ProcessProto called, type = 0, normal stub object
,descriptor_=%{public}s"
,
Str16ToStr8
(
descriptor_
).
c_str
()
);
if
(
!
reply
.
WriteUint32
(
IRemoteObject
::
IF_PROT_BINDER
)
||
!
reply
.
WriteString16
(
descriptor_
)
)
{
ZLOGE
(
LABEL
,
"write to parcel fail"
);
result
=
IPC_STUB_WRITE_PARCEL_ERR
;
}
...
...
ipc/native/src/napi_common/include/napi_remote_object_holder.h
浏览文件 @
ce876990
...
...
@@ -26,13 +26,13 @@
namespace
OHOS
{
class
NAPIRemoteObjectHolder
:
public
RefBase
{
public:
explicit
NAPIRemoteObjectHolder
(
napi_env
env
,
const
std
::
u16string
&
descriptor
);
explicit
NAPIRemoteObjectHolder
(
napi_env
env
,
const
std
::
u16string
&
descriptor
,
napi_value
thisVar
);
~
NAPIRemoteObjectHolder
();
sptr
<
NAPIRemoteObject
>
Get
(
napi_value
object
);
sptr
<
NAPIRemoteObject
>
Get
();
void
Set
(
sptr
<
NAPIRemoteObject
>
object
);
void
attachLocalInterface
(
napi_value
localInterface
,
std
::
string
&
descriptor
);
napi_value
queryLocalInterface
(
std
::
string
&
descriptor
);
/*
void Lock()
void
Lock
()
{
mutex_
.
lock
();
};
...
...
@@ -58,7 +58,7 @@ public:
std
::
u16string
GetDescriptor
()
{
return
descriptor_
;
};
*/
};
private:
std
::
mutex
mutex_
;
...
...
@@ -67,7 +67,8 @@ private:
sptr
<
NAPIRemoteObject
>
sptrCachedObject_
;
wptr
<
NAPIRemoteObject
>
wptrCachedObject_
;
napi_ref
localInterfaceRef_
;
//int32_t attachCount_;
int32_t
attachCount_
;
napi_ref
jsObjectRef_
=
nullptr
;
};
}
// namespace OHOS
#endif // NAPI_REMOTE_OBJECT_HOLDER_H
\ No newline at end of file
ipc/native/src/napi_common/include/napi_remote_object_internal.h
浏览文件 @
ce876990
...
...
@@ -26,7 +26,7 @@
namespace
OHOS
{
class
NAPIRemoteObject
:
public
IPCObjectStub
{
public:
NAPIRemoteObject
(
napi_env
env
,
napi_
value
thisVar
,
const
std
::
u16string
&
descriptor
);
NAPIRemoteObject
(
napi_env
env
,
napi_
ref
jsObjectRef
,
const
std
::
u16string
&
descriptor
);
~
NAPIRemoteObject
()
override
;
...
...
@@ -38,6 +38,7 @@ public:
napi_ref
GetJsObjectRef
()
const
;
private:
std
::
u16string
desc_
;
napi_env
env_
=
nullptr
;
napi_value
thisVar_
=
nullptr
;
static
napi_value
ThenCallback
(
napi_env
env
,
napi_callback_info
info
);
...
...
ipc/native/src/napi_common/source/napi_remote_object.cpp
浏览文件 @
ce876990
...
...
@@ -31,7 +31,7 @@
#include "napi_rpc_error.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
//
#include "native_engine/native_value.h"
#include "native_engine/native_value.h"
namespace
OHOS
{
static
constexpr
OHOS
::
HiviewDFX
::
HiLogLabel
LOG_LABEL
=
{
LOG_CORE
,
LOG_ID_IPC
,
"napi_remoteObject"
};
...
...
@@ -45,7 +45,7 @@ static const uint64_t HITRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag.
static
std
::
atomic
<
int32_t
>
bytraceId
=
1000
;
static
NapiError
napiErr
;
/*
template<class T>
template
<
class
T
>
inline
T
*
ConvertNativeValueTo
(
NativeValue
*
value
)
{
return
(
value
!=
nullptr
)
?
static_cast
<
T
*>
(
value
->
GetInterface
(
T
::
INTERFACE_ID
))
:
nullptr
;
...
...
@@ -60,6 +60,7 @@ static void RemoteObjectHolderFinalizeCb(napi_env env, void *data, void *hint)
}
holder
->
Lock
();
int32_t
curAttachCount
=
holder
->
DecAttachCount
();
ZLOGW
(
LOG_LABEL
,
"NAPIRemoteObjectHolder destructed by js callback, curAttachCount:%{public}d"
,
curAttachCount
);
if
(
curAttachCount
==
0
)
{
delete
holder
;
}
...
...
@@ -111,7 +112,7 @@ static NativeValue *RemoteObjectAttachCb(NativeEngine *engine, void *value, void
holder
->
IncAttachCount
();
holder
->
Unlock
();
return
reinterpret_cast
<
NativeValue
*>
(
jsRemoteObject
);
}
*/
}
napi_value
RemoteObject_JS_Constructor
(
napi_env
env
,
napi_callback_info
info
)
{
...
...
@@ -134,35 +135,28 @@ napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info)
napi_get_value_string_utf8
(
env
,
argv
[
0
],
stringValue
,
bufferSize
+
1
,
&
jsStringLength
);
NAPI_ASSERT
(
env
,
jsStringLength
==
bufferSize
,
"string length wrong"
);
std
::
string
descriptor
=
stringValue
;
auto
holder
=
new
NAPIRemoteObjectHolder
(
env
,
Str8ToStr16
(
descriptor
));
/*
auto nativeObj = ConvertNativeValueTo<NativeObject>(reinterpret_cast<NativeValue *>(thisVar));
auto
holder
=
new
NAPIRemoteObjectHolder
(
env
,
Str8ToStr16
(
descriptor
)
,
thisVar
);
auto
nativeObj
=
ConvertNativeValueTo
<
NativeObject
>
(
reinterpret_cast
<
NativeValue
*>
(
thisVar
));
if
(
nativeObj
==
nullptr
)
{
ZLOGE
(
LOG_LABEL
,
"Failed to get RemoteObject native object"
);
delete
holder
;
return
nullptr
;
}
nativeObj->ConvertToNativeBindingObject(env, RemoteObjectDetachCb, RemoteObjectAttachCb, holder, nullptr);
*/
nativeObj
->
ConvertToNativeBindingObject
(
env
,
RemoteObjectDetachCb
,
RemoteObjectAttachCb
,
holder
,
nullptr
);
// connect native object to js thisVar
napi_status
status
=
napi_wrap
(
env
,
thisVar
,
holder
,
[](
napi_env
env
,
void
*
data
,
void
*
hint
)
{
ZLOGI
(
LOG_LABEL
,
"NAPIRemoteObjectHolder destructed by js callback"
);
delete
(
reinterpret_cast
<
NAPIRemoteObjectHolder
*>
(
data
));
},
nullptr
,
nullptr
);
//napi_status status = napi_wrap(env, thisVar, holder, RemoteObjectHolderFinalizeCb, nullptr, nullptr);
napi_status
status
=
napi_wrap
(
env
,
thisVar
,
holder
,
RemoteObjectHolderFinalizeCb
,
nullptr
,
nullptr
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"wrap js RemoteObject and native holder failed"
);
/*if (NAPI_ohos_rpc_getNativeRemoteObject(env, thisVar) == nullptr) {
ZLOGE(LOG_LABEL, "RemoteObject_JS_Constructor create native object failed");
return nullptr;
}*/
return
thisVar
;
}
NAPIRemoteObject
::
NAPIRemoteObject
(
napi_env
env
,
napi_
value
thisVar
,
const
std
::
u16string
&
descriptor
)
NAPIRemoteObject
::
NAPIRemoteObject
(
napi_env
env
,
napi_
ref
jsObjectRef
,
const
std
::
u16string
&
descriptor
)
:
IPCObjectStub
(
descriptor
)
{
env_
=
env
;
desc_
=
descriptor
;
napi_value
thisVar
=
nullptr
;
napi_get_reference_value
(
env
,
jsObjectRef
,
&
thisVar
);
NAPI_ASSERT_RETURN_VOID
(
env
,
thisVar
!=
nullptr
,
"failed to get value from js RemoteObject ref"
);
thisVar_
=
thisVar
;
napi_create_reference
(
env
,
thisVar_
,
1
,
&
thisVarRef_
);
NAPI_ASSERT_RETURN_VOID
(
env
,
thisVarRef_
!=
nullptr
,
"failed to create ref to js RemoteObject"
);
...
...
@@ -170,7 +164,7 @@ NAPIRemoteObject::NAPIRemoteObject(napi_env env, napi_value thisVar, const std::
NAPIRemoteObject
::~
NAPIRemoteObject
()
{
ZLOGI
(
LOG_LABEL
,
"NAPIRemoteObject
Destructor"
);
ZLOGI
(
LOG_LABEL
,
"NAPIRemoteObject
destroyed, desc:%{public}s"
,
Str16ToStr8
(
desc_
).
c_str
()
);
if
(
thisVarRef_
!=
nullptr
)
{
napi_status
status
=
napi_delete_reference
(
env_
,
thisVarRef_
);
NAPI_ASSERT_RETURN_VOID
(
env_
,
status
==
napi_ok
,
"failed to delete ref to js RemoteObject"
);
...
...
@@ -603,9 +597,8 @@ napi_value NAPI_ohos_rpc_CreateJsRemoteObject(napi_env env, const sptr<IRemoteOb
if
(
target
->
CheckObjectLegality
())
{
IPCObjectStub
*
tmp
=
static_cast
<
IPCObjectStub
*>
(
target
.
GetRefPtr
());
uint32_t
objectType
=
tmp
->
GetObjectType
();
ZLOGI
(
LOG_LABEL
,
"
[mgttest]
object type:%{public}d"
,
objectType
);
ZLOGI
(
LOG_LABEL
,
"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
());
// retrieve js remote object constructor
napi_value
global
=
nullptr
;
...
...
@@ -630,7 +623,6 @@ 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
;
}
...
...
@@ -668,7 +660,7 @@ sptr<IRemoteObject> NAPI_ohos_rpc_getNativeRemoteObject(napi_env env, napi_value
NAPIRemoteObjectHolder
*
holder
=
nullptr
;
napi_unwrap
(
env
,
object
,
(
void
**
)
&
holder
);
NAPI_ASSERT
(
env
,
holder
!=
nullptr
,
"failed to get napi remote object holder"
);
return
holder
!=
nullptr
?
holder
->
Get
(
object
)
:
nullptr
;
return
holder
!=
nullptr
?
holder
->
Get
()
:
nullptr
;
}
napi_value
proxyConstructor
=
nullptr
;
...
...
ipc/native/src/napi_common/source/napi_remote_object_holder.cpp
浏览文件 @
ce876990
...
...
@@ -18,10 +18,17 @@
#include <string_ex.h>
namespace
OHOS
{
NAPIRemoteObjectHolder
::
NAPIRemoteObjectHolder
(
napi_env
env
,
const
std
::
u16string
&
descriptor
)
:
env_
(
env
),
descriptor_
(
descriptor
),
cachedObject_
(
nullptr
),
localInterfaceRef_
(
nullptr
)
// : env_(env), descriptor_(descriptor), cachedObject_(nullptr), localInterfaceRef_(nullptr), attachCount_(1)
{}
NAPIRemoteObjectHolder
::
NAPIRemoteObjectHolder
(
napi_env
env
,
const
std
::
u16string
&
descriptor
,
napi_value
thisVar
)
{
ZLOGE
(
LOG_LABEL
,
"[memtest]NAPIRemoteObjectHolder created"
);
env_
=
env
;
descriptor_
=
descriptor
;
sptrCachedObject_
=
nullptr
;
wptrCachedObject_
=
nullptr
;
localInterfaceRef_
=
nullptr
;
attachCount_
=
1
;
napi_create_reference
(
env
,
thisVar
,
0
,
&
jsObjectRef_
);
}
NAPIRemoteObjectHolder
::~
NAPIRemoteObjectHolder
()
{
...
...
@@ -33,7 +40,7 @@ NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder()
}
}
sptr
<
NAPIRemoteObject
>
NAPIRemoteObjectHolder
::
Get
(
napi_value
jsRemoteObject
)
sptr
<
NAPIRemoteObject
>
NAPIRemoteObjectHolder
::
Get
()
{
std
::
lock_guard
<
std
::
mutex
>
lockGuard
(
mutex_
);
// grab an strong reference to the object,
...
...
@@ -43,7 +50,7 @@ sptr<NAPIRemoteObject> NAPIRemoteObjectHolder::Get(napi_value jsRemoteObject)
}
sptr
<
NAPIRemoteObject
>
tmp
=
wptrCachedObject_
.
promote
();
if
(
tmp
==
nullptr
)
{
tmp
=
new
NAPIRemoteObject
(
env_
,
js
RemoteObject
,
descriptor_
);
tmp
=
new
NAPIRemoteObject
(
env_
,
js
ObjectRef_
,
descriptor_
);
wptrCachedObject_
=
tmp
;
}
return
tmp
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录