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

NAPIRemoteObject内存泄露修改

Signed-off-by: N马根堂 <magentang4@huawei.com>
上级 4a82acc0
......@@ -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: {
......
......@@ -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;
}
......
......@@ -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
......@@ -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);
......
......@@ -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;
......
......@@ -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_, jsRemoteObject, descriptor_);
tmp = new NAPIRemoteObject(env_, jsObjectRef_, descriptor_);
wptrCachedObject_ = tmp;
}
return tmp;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册