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

Commit 557ffb1

Browse files
authored
Migrate FlutterDartVMServicePublisher to ARC (#52081)
Smart pointers support ARC as of #47612, and the unit tests were migrated in #48162. Migrate `FlutterDartVMServicePublisher` from MRC to ARC. I validated `flutter attach` works on this engine PR, so the VM service URL is being advertised and the tool is discovering it. Part of flutter/flutter#137801.
1 parent 503e7e8 commit 557ffb1

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

shell/platform/darwin/ios/BUILD.gn

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ source_set("flutter_framework_source_arc") {
6161
sources = [
6262
"framework/Source/FlutterCallbackCache.mm",
6363
"framework/Source/FlutterCallbackCache_Internal.h",
64+
"framework/Source/FlutterDartVMServicePublisher.h",
65+
"framework/Source/FlutterDartVMServicePublisher.mm",
6466
"framework/Source/FlutterEmbedderKeyResponder.h",
6567
"framework/Source/FlutterEmbedderKeyResponder.mm",
6668
"framework/Source/FlutterKeyPrimaryResponder.h",
@@ -89,6 +91,7 @@ source_set("flutter_framework_source_arc") {
8991

9092
deps += [
9193
"//flutter/lib/ui",
94+
"//flutter/runtime",
9295
"//flutter/shell/platform/embedder:embedder_as_internal_library",
9396
]
9497
}
@@ -109,8 +112,6 @@ source_set("flutter_framework_source") {
109112
"framework/Source/FlutterChannelKeyResponder.mm",
110113
"framework/Source/FlutterDartProject.mm",
111114
"framework/Source/FlutterDartProject_Internal.h",
112-
"framework/Source/FlutterDartVMServicePublisher.h",
113-
"framework/Source/FlutterDartVMServicePublisher.mm",
114115
"framework/Source/FlutterEngine.mm",
115116
"framework/Source/FlutterEngineGroup.mm",
116117
"framework/Source/FlutterEngine_Internal.h",

shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
- (instancetype)init NS_UNAVAILABLE;
1515
+ (instancetype)new NS_UNAVAILABLE;
1616

17-
@property(nonatomic, retain, readonly) NSURL* url;
17+
@property(nonatomic, readonly) NSURL* url;
1818

1919
@end
2020

shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.mm

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ - (instancetype)initWithEnableVMServicePublication:(BOOL)enableVMServicePublicat
4040
#include <net/if.h>
4141

4242
#include "flutter/fml/logging.h"
43-
#include "flutter/fml/memory/weak_ptr.h"
4443
#include "flutter/fml/message_loop.h"
4544
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
4645
#include "flutter/runtime/dart_service_isolate.h"
46+
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
47+
48+
FLUTTER_ASSERT_ARC
4749

4850
@protocol FlutterDartVMServicePublisherDelegate
4951
- (void)publishServiceProtocolPort:(NSURL*)uri;
@@ -54,7 +56,7 @@ @interface FlutterDartVMServicePublisher ()
5456
+ (NSData*)createTxtData:(NSURL*)url;
5557

5658
@property(readonly, class) NSString* serviceName;
57-
@property(readonly) fml::scoped_nsobject<NSObject<FlutterDartVMServicePublisherDelegate>> delegate;
59+
@property(readonly) NSObject<FlutterDartVMServicePublisherDelegate>* delegate;
5860
@property(nonatomic, readwrite) NSURL* url;
5961
@property(readonly) BOOL enableVMServicePublication;
6062

@@ -139,32 +141,31 @@ static void DNSSD_API RegistrationCallback(DNSServiceRef sdRef,
139141

140142
@implementation FlutterDartVMServicePublisher {
141143
flutter::DartServiceIsolate::CallbackHandle _callbackHandle;
142-
std::unique_ptr<fml::WeakPtrFactory<FlutterDartVMServicePublisher>> _weakFactory;
143144
}
144145

145146
- (instancetype)initWithEnableVMServicePublication:(BOOL)enableVMServicePublication {
146147
self = [super init];
147148
NSAssert(self, @"Super must not return null on init.");
148149

149-
_delegate.reset([[DartVMServiceDNSServiceDelegate alloc] init]);
150+
_delegate = [[DartVMServiceDNSServiceDelegate alloc] init];
150151
_enableVMServicePublication = enableVMServicePublication;
151-
_weakFactory = std::make_unique<fml::WeakPtrFactory<FlutterDartVMServicePublisher>>(self);
152+
__weak __typeof(self) weakSelf = self;
152153

153154
fml::MessageLoop::EnsureInitializedForCurrentThread();
154155

155156
_callbackHandle = flutter::DartServiceIsolate::AddServerStatusCallback(
156-
[weak = _weakFactory->GetWeakPtr(),
157-
runner = fml::MessageLoop::GetCurrent().GetTaskRunner()](const std::string& uri) {
157+
[weakSelf, runner = fml::MessageLoop::GetCurrent().GetTaskRunner()](const std::string& uri) {
158158
if (!uri.empty()) {
159-
runner->PostTask([weak, uri]() {
159+
runner->PostTask([weakSelf, uri]() {
160+
FlutterDartVMServicePublisher* strongSelf = weakSelf;
160161
// uri comes in as something like 'http://127.0.0.1:XXXXX/' where XXXXX is the port
161162
// number.
162-
if (weak) {
163-
NSURL* url = [[[NSURL alloc]
164-
initWithString:[NSString stringWithUTF8String:uri.c_str()]] autorelease];
165-
weak.get().url = url;
166-
if (weak.get().enableVMServicePublication) {
167-
[[weak.get() delegate] publishServiceProtocolPort:url];
163+
if (strongSelf) {
164+
NSURL* url =
165+
[[NSURL alloc] initWithString:[NSString stringWithUTF8String:uri.c_str()]];
166+
strongSelf.url = url;
167+
if (strongSelf.enableVMServicePublication) {
168+
[[strongSelf delegate] publishServiceProtocolPort:url];
168169
}
169170
}
170171
});
@@ -190,15 +191,9 @@ + (NSData*)createTxtData:(NSURL*)url {
190191
}
191192

192193
- (void)dealloc {
193-
// It will be destroyed and invalidate its weak pointers
194-
// before any other members are destroyed.
195-
_weakFactory.reset();
196-
197194
[_delegate stopService];
198-
[_url release];
199195

200196
flutter::DartServiceIsolate::RemoveServerStatusCallback(_callbackHandle);
201-
[super dealloc];
202197
}
203198
@end
204199

0 commit comments

Comments
 (0)