!1525 修改lambda表达式的捕获模式

Merge pull request !1525 from hanlin15/master_lambda
This commit is contained in:
openharmony_ci 2024-11-19 11:49:50 +00:00 committed by Gitee
commit 16e70026fe
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 32 additions and 21 deletions

View File

@ -42,7 +42,7 @@ void NAPIDeathRecipient::AfterWorkCallback(uv_work_t *work, int status)
napi_handle_scope scope = nullptr;
napi_open_handle_scope(param->env, &scope);
auto CleanUp = [&]() {
auto CleanUp = [&param, &scope, &work]() {
napi_close_handle_scope(param->env, scope);
delete param;
delete work;

View File

@ -52,7 +52,7 @@ HWTEST_F(InvokerFactoryTest, Register001, TestSize.Level1)
int protocol = 1;
IRemoteInvoker* invoker = nullptr;
auto invokerObject = [&invoker]() -> IRemoteInvoker* {
auto creator = [&invoker]() -> IRemoteInvoker* {
invoker = new (std::nothrow) BinderInvoker();
if (invoker == nullptr) {
return nullptr;
@ -60,7 +60,7 @@ HWTEST_F(InvokerFactoryTest, Register001, TestSize.Level1)
return invoker;
};
bool ret = invokerFactory.Register(protocol, invokerObject);
bool ret = invokerFactory.Register(protocol, creator);
EXPECT_EQ(ret, false);
if (invoker != nullptr) {
delete invoker;
@ -68,7 +68,7 @@ HWTEST_F(InvokerFactoryTest, Register001, TestSize.Level1)
}
invokerFactory.isAvailable_ = true;
ret = invokerFactory.Register(protocol, invokerObject);
ret = invokerFactory.Register(protocol, creator);
EXPECT_EQ(ret, false);
IRemoteInvoker* iRemoteInvoker = invokerFactory.newInstance(protocol);
EXPECT_NE(iRemoteInvoker, nullptr);
@ -76,6 +76,9 @@ HWTEST_F(InvokerFactoryTest, Register001, TestSize.Level1)
delete invoker;
invoker = nullptr;
}
// after leaving the scope, the captured 'invoker' object will be invalid in 'creator' lambda expression
// so we need to delete 'creator' lambda expression
invokerFactory.Unregister(protocol);
}
/**

View File

@ -56,8 +56,8 @@ int FooStub::WaitForAsyncReply(int timeout)
{
asyncReply_ = 0;
std::unique_lock<std::mutex> lck(mutex_);
cv_.wait_for(lck, std::chrono::milliseconds(timeout), [&]() {
return asyncReply_ != 0;
cv_.wait_for(lck, std::chrono::milliseconds(timeout), [this]() {
return this->asyncReply_ != 0;
});
return asyncReply_;
}

View File

@ -211,8 +211,8 @@ int NativeRemoteProxyTest::WaitForAsyncReply(int timeout)
{
asyncReply_ = 0;
std::unique_lock<std::mutex> lck(mutex_);
cv_.wait_for(lck, std::chrono::seconds(timeout), [&]() {
return asyncReply_ != 0;
cv_.wait_for(lck, std::chrono::seconds(timeout), [this]() {
return this->asyncReply_ != 0;
});
return asyncReply_;
}
@ -478,7 +478,7 @@ int NativeRemoteProxyTest::SendErrorCode()
if (proxy_ == nullptr) {
return -1;
}
auto func = [&, this](int val, int expect) -> int {
auto func = [proxy = this->proxy_](int val, int expect) -> int {
OHIPCParcel *data = OH_IPCParcel_Create();
if (data == nullptr) {
return -1;
@ -495,7 +495,7 @@ int NativeRemoteProxyTest::SendErrorCode()
return -1;
}
OH_IPCParcel_WriteInt32(data, val);
int ret = OH_IPCRemoteProxy_SendRequest(this->proxy_, NATIVE_TEST_CMD_SEND_ERROR_CODE, data, reply, &option);
int ret = OH_IPCRemoteProxy_SendRequest(proxy, NATIVE_TEST_CMD_SEND_ERROR_CODE, data, reply, &option);
OH_IPCParcel_Destroy(data);
OH_IPCParcel_Destroy(reply);
return (ret == expect) ? 0 : -1;

View File

@ -196,15 +196,15 @@ bool TestServiceClient::TestNativeIPCSendRequests(int subCmd)
}
static std::map<int, std::function<int()>> commandMap = {
{ NATIVE_TEST_CMD_SYNC_ADD, [&]() { return remoteProxy->SyncAdd(); }},
{ NATIVE_TEST_CMD_ASYNC_ADD, [&]() { return remoteProxy->ASyncAdd(); }},
{ NATIVE_TEST_CMD_SYNC_ADD_REPEAT, [&]() { return remoteProxy->AddParallel(true); }},
{ NATIVE_TEST_CMD_ASYNC_ADD_REPEAT, [&]() { return remoteProxy->AddParallel(false); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_BASE, [&]() { return remoteProxy->SendAndEchoBase(); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_SRING, [&]() { return remoteProxy->SendAndEchoString(); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_BUFFER, [&]() { return remoteProxy->SendAndEchoBuffer(); }},
{ NATIVE_TEST_CMD_SEND_FILE_DESCRIPTOR, [&]() { return remoteProxy->SendAndEchoFileDescriptor(); }},
{ NATIVE_TEST_CMD_SEND_ERROR_CODE, [&]() { return remoteProxy->SendErrorCode(); }},
{ NATIVE_TEST_CMD_SYNC_ADD, [remoteProxy]() { return remoteProxy->SyncAdd(); }},
{ NATIVE_TEST_CMD_ASYNC_ADD, [remoteProxy]() { return remoteProxy->ASyncAdd(); }},
{ NATIVE_TEST_CMD_SYNC_ADD_REPEAT, [remoteProxy]() { return remoteProxy->AddParallel(true); }},
{ NATIVE_TEST_CMD_ASYNC_ADD_REPEAT, [remoteProxy]() { return remoteProxy->AddParallel(false); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_BASE, [remoteProxy]() { return remoteProxy->SendAndEchoBase(); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_SRING, [remoteProxy]() { return remoteProxy->SendAndEchoString(); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_BUFFER, [remoteProxy]() { return remoteProxy->SendAndEchoBuffer(); }},
{ NATIVE_TEST_CMD_SEND_FILE_DESCRIPTOR, [remoteProxy]() { return remoteProxy->SendAndEchoFileDescriptor(); }},
{ NATIVE_TEST_CMD_SEND_ERROR_CODE, [remoteProxy]() { return remoteProxy->SendErrorCode(); }},
};
auto it = commandMap.find(subCmd);
if (it == commandMap.end()) {

View File

@ -857,7 +857,14 @@ bool DBinderService::RegisterRemoteProxyInner(std::u16string serviceName, binder
void DBinderService::AddAsynMessageTask(std::shared_ptr<struct DHandleEntryTxRx> message)
{
auto task = [this, message] { this->OnRemoteMessageTask(message); };
sptr<DBinderService> servicePtr = this;
auto task = [servicePtr, message] {
if (servicePtr == nullptr) {
DBINDER_LOGE(LOG_LABEL, "invalid dbinder service object");
return;
}
servicePtr->OnRemoteMessageTask(message);
};
ffrt::submit(task);
}

View File

@ -109,7 +109,8 @@ void DbinderTest::SetUp()
bool DbinderTest::GetRemoteDeviceId()
{
std::string msg = "Ask Device ID";
int ret = SendMessage(AGENT_NO::ONE, msg, strlen(msg.c_str()), [&](const std::string &retId, int retLen) -> bool {
int ret = SendMessage(AGENT_NO::ONE, msg,
strlen(msg.c_str()), [serverId_](const std::string &retId, int retLen) -> bool {
if (memcpy_s(serverId_, DEVICEID_LENGTH, retId.c_str(), DEVICEID_LENGTH) != 0 || retLen != DEVICEID_LENGTH) {
DBINDER_LOGE(LOG_LABEL, "fail to copy string");
return false;