Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2cd19e3

Browse files
Wrap the iOS platform message handler in an autorelease pool block (#40373)
Wrap the iOS platform message handler in an autorelease pool block
1 parent 56727d6 commit 2cd19e3

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

shell/platform/darwin/ios/platform_message_handler_ios.mm

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,50 +46,52 @@ - (void)dispatch:(dispatch_block_t)block {
4646

4747
void PlatformMessageHandlerIos::HandlePlatformMessage(std::unique_ptr<PlatformMessage> message) {
4848
// This can be called from any isolate's thread.
49-
fml::RefPtr<flutter::PlatformMessageResponse> completer = message->response();
50-
HandlerInfo handler_info;
51-
{
52-
// TODO(gaaclarke): This mutex is a bottleneck for multiple isolates sending
53-
// messages at the same time. This could be potentially changed to a
54-
// read-write lock.
55-
std::lock_guard lock(message_handlers_mutex_);
56-
auto it = message_handlers_.find(message->channel());
57-
if (it != message_handlers_.end()) {
58-
handler_info = it->second;
49+
@autoreleasepool {
50+
fml::RefPtr<flutter::PlatformMessageResponse> completer = message->response();
51+
HandlerInfo handler_info;
52+
{
53+
// TODO(gaaclarke): This mutex is a bottleneck for multiple isolates sending
54+
// messages at the same time. This could be potentially changed to a
55+
// read-write lock.
56+
std::lock_guard lock(message_handlers_mutex_);
57+
auto it = message_handlers_.find(message->channel());
58+
if (it != message_handlers_.end()) {
59+
handler_info = it->second;
60+
}
5961
}
60-
}
61-
if (handler_info.handler) {
62-
FlutterBinaryMessageHandler handler = handler_info.handler;
63-
NSData* data = nil;
64-
if (message->hasData()) {
65-
data = ConvertMappingToNSData(message->releaseData());
66-
}
67-
68-
uint64_t platform_message_id = platform_message_counter++;
69-
TRACE_EVENT_ASYNC_BEGIN1("flutter", "PlatformChannel ScheduleHandler", platform_message_id,
70-
"channel", message->channel().c_str());
71-
dispatch_block_t run_handler = ^{
72-
handler(data, ^(NSData* reply) {
73-
TRACE_EVENT_ASYNC_END0("flutter", "PlatformChannel ScheduleHandler", platform_message_id);
74-
// Called from any thread.
75-
if (completer) {
76-
if (reply) {
77-
completer->Complete(ConvertNSDataToMappingPtr(reply));
78-
} else {
79-
completer->CompleteEmpty();
62+
if (handler_info.handler) {
63+
FlutterBinaryMessageHandler handler = handler_info.handler;
64+
NSData* data = nil;
65+
if (message->hasData()) {
66+
data = ConvertMappingToNSData(message->releaseData());
67+
}
68+
69+
uint64_t platform_message_id = platform_message_counter++;
70+
TRACE_EVENT_ASYNC_BEGIN1("flutter", "PlatformChannel ScheduleHandler", platform_message_id,
71+
"channel", message->channel().c_str());
72+
dispatch_block_t run_handler = ^{
73+
handler(data, ^(NSData* reply) {
74+
TRACE_EVENT_ASYNC_END0("flutter", "PlatformChannel ScheduleHandler", platform_message_id);
75+
// Called from any thread.
76+
if (completer) {
77+
if (reply) {
78+
completer->Complete(ConvertNSDataToMappingPtr(reply));
79+
} else {
80+
completer->CompleteEmpty();
81+
}
8082
}
81-
}
82-
});
83-
};
84-
85-
if (handler_info.task_queue.get()) {
86-
[handler_info.task_queue.get() dispatch:run_handler];
83+
});
84+
};
85+
86+
if (handler_info.task_queue.get()) {
87+
[handler_info.task_queue.get() dispatch:run_handler];
88+
} else {
89+
dispatch_async(dispatch_get_main_queue(), run_handler);
90+
}
8791
} else {
88-
dispatch_async(dispatch_get_main_queue(), run_handler);
89-
}
90-
} else {
91-
if (completer) {
92-
completer->CompleteEmpty();
92+
if (completer) {
93+
completer->CompleteEmpty();
94+
}
9395
}
9496
}
9597
}

0 commit comments

Comments
 (0)