提交 ce876990 编写于 作者: 马根堂

NAPIRemoteObject内存泄露修改

Signed-off-by: N马根堂 <magentang4@huawei.com>
上级 4a82acc0
...@@ -530,6 +530,9 @@ int IPCObjectProxy::GetProtoInfo() ...@@ -530,6 +530,9 @@ int IPCObjectProxy::GetProtoInfo()
switch (reply.ReadUint32()) { switch (reply.ReadUint32()) {
case IRemoteObject::IF_PROT_BINDER: { 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; break;
} }
case IRemoteObject::IF_PROT_DATABUS: { case IRemoteObject::IF_PROT_DATABUS: {
......
...@@ -371,8 +371,8 @@ int IPCObjectStub::GetObjectType() const ...@@ -371,8 +371,8 @@ int IPCObjectStub::GetObjectType() const
int32_t IPCObjectStub::ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) int32_t IPCObjectStub::ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{ {
int result = ERR_NONE; int result = ERR_NONE;
ZLOGD(LABEL, "IPCObjectStub::ProcessProto called, type = 0, normal stub object"); ZLOGD(LABEL, "IPCObjectStub::ProcessProto called, type = 0, normal stub object,descriptor_=%{public}s",Str16ToStr8(descriptor_).c_str());
if (!reply.WriteUint32(IRemoteObject::IF_PROT_BINDER)) { if (!reply.WriteUint32(IRemoteObject::IF_PROT_BINDER) || !reply.WriteString16(descriptor_)) {
ZLOGE(LABEL, "write to parcel fail"); ZLOGE(LABEL, "write to parcel fail");
result = IPC_STUB_WRITE_PARCEL_ERR; result = IPC_STUB_WRITE_PARCEL_ERR;
} }
......
...@@ -26,13 +26,13 @@ ...@@ -26,13 +26,13 @@
namespace OHOS { namespace OHOS {
class NAPIRemoteObjectHolder : public RefBase { class NAPIRemoteObjectHolder : public RefBase {
public: public:
explicit NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor); explicit NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor, napi_value thisVar);
~NAPIRemoteObjectHolder(); ~NAPIRemoteObjectHolder();
sptr<NAPIRemoteObject> Get(napi_value object); sptr<NAPIRemoteObject> Get();
void Set(sptr<NAPIRemoteObject> object); void Set(sptr<NAPIRemoteObject> object);
void attachLocalInterface(napi_value localInterface, std::string &descriptor); void attachLocalInterface(napi_value localInterface, std::string &descriptor);
napi_value queryLocalInterface(std::string &descriptor); napi_value queryLocalInterface(std::string &descriptor);
/*void Lock() void Lock()
{ {
mutex_.lock(); mutex_.lock();
}; };
...@@ -58,7 +58,7 @@ public: ...@@ -58,7 +58,7 @@ public:
std::u16string GetDescriptor() std::u16string GetDescriptor()
{ {
return descriptor_; return descriptor_;
};*/ };
private: private:
std::mutex mutex_; std::mutex mutex_;
...@@ -67,7 +67,8 @@ private: ...@@ -67,7 +67,8 @@ private:
sptr<NAPIRemoteObject> sptrCachedObject_; sptr<NAPIRemoteObject> sptrCachedObject_;
wptr<NAPIRemoteObject> wptrCachedObject_; wptr<NAPIRemoteObject> wptrCachedObject_;
napi_ref localInterfaceRef_; napi_ref localInterfaceRef_;
//int32_t attachCount_; int32_t attachCount_;
napi_ref jsObjectRef_ = nullptr;
}; };
} // namespace OHOS } // namespace OHOS
#endif // NAPI_REMOTE_OBJECT_HOLDER_H #endif // NAPI_REMOTE_OBJECT_HOLDER_H
\ No newline at end of file
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
namespace OHOS { namespace OHOS {
class NAPIRemoteObject : public IPCObjectStub { class NAPIRemoteObject : public IPCObjectStub {
public: 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; ~NAPIRemoteObject() override;
...@@ -38,6 +38,7 @@ public: ...@@ -38,6 +38,7 @@ public:
napi_ref GetJsObjectRef() const; napi_ref GetJsObjectRef() const;
private: private:
std::u16string desc_;
napi_env env_ = nullptr; napi_env env_ = nullptr;
napi_value thisVar_ = nullptr; napi_value thisVar_ = nullptr;
static napi_value ThenCallback(napi_env env, napi_callback_info info); static napi_value ThenCallback(napi_env env, napi_callback_info info);
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
#include "napi_rpc_error.h" #include "napi_rpc_error.h"
#include "napi/native_api.h" #include "napi/native_api.h"
#include "napi/native_node_api.h" #include "napi/native_node_api.h"
//#include "native_engine/native_value.h" #include "native_engine/native_value.h"
namespace OHOS { namespace OHOS {
static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "napi_remoteObject" }; 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. ...@@ -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 std::atomic<int32_t> bytraceId = 1000;
static NapiError napiErr; static NapiError napiErr;
/*template<class T> template<class T>
inline T *ConvertNativeValueTo(NativeValue *value) inline T *ConvertNativeValueTo(NativeValue *value)
{ {
return (value != nullptr) ? static_cast<T *>(value->GetInterface(T::INTERFACE_ID)) : nullptr; 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) ...@@ -60,6 +60,7 @@ static void RemoteObjectHolderFinalizeCb(napi_env env, void *data, void *hint)
} }
holder->Lock(); holder->Lock();
int32_t curAttachCount = holder->DecAttachCount(); int32_t curAttachCount = holder->DecAttachCount();
ZLOGW(LOG_LABEL, "NAPIRemoteObjectHolder destructed by js callback, curAttachCount:%{public}d", curAttachCount);
if (curAttachCount == 0) { if (curAttachCount == 0) {
delete holder; delete holder;
} }
...@@ -111,7 +112,7 @@ static NativeValue *RemoteObjectAttachCb(NativeEngine *engine, void *value, void ...@@ -111,7 +112,7 @@ static NativeValue *RemoteObjectAttachCb(NativeEngine *engine, void *value, void
holder->IncAttachCount(); holder->IncAttachCount();
holder->Unlock(); holder->Unlock();
return reinterpret_cast<NativeValue *>(jsRemoteObject); return reinterpret_cast<NativeValue *>(jsRemoteObject);
}*/ }
napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info) 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) ...@@ -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_get_value_string_utf8(env, argv[0], stringValue, bufferSize + 1, &jsStringLength);
NAPI_ASSERT(env, jsStringLength == bufferSize, "string length wrong"); NAPI_ASSERT(env, jsStringLength == bufferSize, "string length wrong");
std::string descriptor = stringValue; std::string descriptor = stringValue;
auto holder = new NAPIRemoteObjectHolder(env, Str8ToStr16(descriptor)); auto holder = new NAPIRemoteObjectHolder(env, Str8ToStr16(descriptor), thisVar);
/*auto nativeObj = ConvertNativeValueTo<NativeObject>(reinterpret_cast<NativeValue *>(thisVar)); auto nativeObj = ConvertNativeValueTo<NativeObject>(reinterpret_cast<NativeValue *>(thisVar));
if (nativeObj == nullptr) { if (nativeObj == nullptr) {
ZLOGE(LOG_LABEL, "Failed to get RemoteObject native object"); ZLOGE(LOG_LABEL, "Failed to get RemoteObject native object");
delete holder; delete holder;
return nullptr; return nullptr;
} }
nativeObj->ConvertToNativeBindingObject(env, RemoteObjectDetachCb, RemoteObjectAttachCb, holder, nullptr);*/ nativeObj->ConvertToNativeBindingObject(env, RemoteObjectDetachCb, RemoteObjectAttachCb, holder, nullptr);
// connect native object to js thisVar // connect native object to js thisVar
napi_status status = napi_wrap( napi_status status = napi_wrap(env, thisVar, holder, RemoteObjectHolderFinalizeCb, nullptr, nullptr);
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_ASSERT(env, status == napi_ok, "wrap js RemoteObject and native holder failed"); 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; 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) : IPCObjectStub(descriptor)
{ {
env_ = env; 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; thisVar_ = thisVar;
napi_create_reference(env, thisVar_, 1, &thisVarRef_); napi_create_reference(env, thisVar_, 1, &thisVarRef_);
NAPI_ASSERT_RETURN_VOID(env, thisVarRef_ != nullptr, "failed to create ref to js RemoteObject"); 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:: ...@@ -170,7 +164,7 @@ NAPIRemoteObject::NAPIRemoteObject(napi_env env, napi_value thisVar, const std::
NAPIRemoteObject::~NAPIRemoteObject() NAPIRemoteObject::~NAPIRemoteObject()
{ {
ZLOGI(LOG_LABEL, "NAPIRemoteObject Destructor"); ZLOGI(LOG_LABEL, "NAPIRemoteObject destroyed, desc:%{public}s", Str16ToStr8(desc_).c_str());
if (thisVarRef_ != nullptr) { if (thisVarRef_ != nullptr) {
napi_status status = napi_delete_reference(env_, thisVarRef_); napi_status status = napi_delete_reference(env_, thisVarRef_);
NAPI_ASSERT_RETURN_VOID(env_, status == napi_ok, "failed to delete ref to js RemoteObject"); 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 ...@@ -603,9 +597,8 @@ 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());
uint32_t objectType = tmp->GetObjectType(); 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) { 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()); sptr<NAPIRemoteObject> object = static_cast<NAPIRemoteObject *>(target.GetRefPtr());
// retrieve js remote object constructor // retrieve js remote object constructor
napi_value global = nullptr; napi_value global = nullptr;
...@@ -630,7 +623,6 @@ napi_value NAPI_ohos_rpc_CreateJsRemoteObject(napi_env env, const sptr<IRemoteOb ...@@ -630,7 +623,6 @@ 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;
} }
...@@ -668,7 +660,7 @@ sptr<IRemoteObject> NAPI_ohos_rpc_getNativeRemoteObject(napi_env env, napi_value ...@@ -668,7 +660,7 @@ sptr<IRemoteObject> NAPI_ohos_rpc_getNativeRemoteObject(napi_env env, napi_value
NAPIRemoteObjectHolder *holder = nullptr; NAPIRemoteObjectHolder *holder = nullptr;
napi_unwrap(env, object, (void **)&holder); napi_unwrap(env, object, (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");
return holder != nullptr ? holder->Get(object) : nullptr; return holder != nullptr ? holder->Get() : nullptr;
} }
napi_value proxyConstructor = nullptr; napi_value proxyConstructor = nullptr;
......
...@@ -18,10 +18,17 @@ ...@@ -18,10 +18,17 @@
#include <string_ex.h> #include <string_ex.h>
namespace OHOS { namespace OHOS {
NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor) NAPIRemoteObjectHolder::NAPIRemoteObjectHolder(napi_env env, const std::u16string &descriptor, napi_value thisVar)
: env_(env), descriptor_(descriptor), cachedObject_(nullptr), localInterfaceRef_(nullptr) {
// : env_(env), descriptor_(descriptor), cachedObject_(nullptr), localInterfaceRef_(nullptr), attachCount_(1) 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() NAPIRemoteObjectHolder::~NAPIRemoteObjectHolder()
{ {
...@@ -33,7 +40,7 @@ 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_); std::lock_guard<std::mutex> lockGuard(mutex_);
// grab an strong reference to the object, // grab an strong reference to the object,
...@@ -43,7 +50,7 @@ sptr<NAPIRemoteObject> NAPIRemoteObjectHolder::Get(napi_value jsRemoteObject) ...@@ -43,7 +50,7 @@ sptr<NAPIRemoteObject> NAPIRemoteObjectHolder::Get(napi_value jsRemoteObject)
} }
sptr<NAPIRemoteObject> tmp = wptrCachedObject_.promote(); sptr<NAPIRemoteObject> tmp = wptrCachedObject_.promote();
if (tmp == nullptr) { if (tmp == nullptr) {
tmp = new NAPIRemoteObject(env_, jsRemoteObject, descriptor_); tmp = new NAPIRemoteObject(env_, jsObjectRef_, descriptor_);
wptrCachedObject_ = tmp; wptrCachedObject_ = tmp;
} }
return tmp; return tmp;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册