29
29
30
30
NSString * const FlutterDefaultDartEntrypoint = nil ;
31
31
32
+ @interface FlutterEngineRegistrar : NSObject <FlutterPluginRegistrar>
33
+ @property (nonatomic , assign ) FlutterEngine* flutterEngine;
34
+ - (instancetype )initWithPlugin : (NSString *)pluginKey flutterEngine : (FlutterEngine*)flutterEngine ;
35
+ @end
36
+
32
37
@interface FlutterEngine () <FlutterTextInputDelegate, FlutterBinaryMessenger>
33
38
// Maintains a dictionary of plugin names that have registered with the engine. Used by
34
39
// FlutterEngineRegistrar to implement a FlutterPluginRegistrar.
35
40
@property (nonatomic , readonly ) NSMutableDictionary * pluginPublications;
41
+ @property (nonatomic , readonly ) NSMutableDictionary <NSString*, FlutterEngineRegistrar*>* registrars;
36
42
37
43
@property (nonatomic , readwrite , copy ) NSString * isolateId;
38
44
@property (nonatomic , retain ) id <NSObject > flutterViewControllerWillDeallocObserver;
39
45
@end
40
46
41
- @interface FlutterEngineRegistrar : NSObject <FlutterPluginRegistrar>
42
- - (instancetype )initWithPlugin : (NSString *)pluginKey flutterEngine : (FlutterEngine*)flutterEngine ;
43
- @end
44
-
45
47
@implementation FlutterEngine {
46
48
fml::scoped_nsobject<FlutterDartProject> _dartProject;
47
49
flutter::ThreadHost _threadHost;
@@ -98,6 +100,7 @@ - (instancetype)initWithName:(NSString*)labelPrefix
98
100
_dartProject.reset ([project retain ]);
99
101
100
102
_pluginPublications = [NSMutableDictionary new ];
103
+ _registrars = [[NSMutableDictionary alloc ] init ];
101
104
_platformViewsController.reset (new flutter::FlutterPlatformViewsController ());
102
105
103
106
_binaryMessenger = [[FlutterBinaryMessengerRelay alloc ] initWithParent: self ];
@@ -122,8 +125,24 @@ - (instancetype)initWithName:(NSString*)labelPrefix
122
125
}
123
126
124
127
- (void )dealloc {
128
+ // / Notify plugins of dealloc. This should happen first in dealloc since the
129
+ // / plugins may be talking to things like the binaryMessenger.
130
+ [_pluginPublications enumerateKeysAndObjectsUsingBlock: ^(id key, id object, BOOL * stop) {
131
+ if ([object respondsToSelector: @selector (detachFromEngineForRegistrar: )]) {
132
+ NSObject <FlutterPluginRegistrar>* registrar = self.registrars [key];
133
+ [object detachFromEngineForRegistrar: registrar];
134
+ }
135
+ }];
136
+
137
+ // / nil out weak references.
138
+ [_registrars
139
+ enumerateKeysAndObjectsUsingBlock: ^(id key, FlutterEngineRegistrar* registrar, BOOL * stop) {
140
+ registrar.flutterEngine = nil ;
141
+ }];
142
+
125
143
[_labelPrefix release ];
126
144
[_pluginPublications release ];
145
+ [_registrars release ];
127
146
_binaryMessenger.parent = nil ;
128
147
[_binaryMessenger release ];
129
148
@@ -647,7 +666,10 @@ - (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
647
666
- (NSObject <FlutterPluginRegistrar>*)registrarForPlugin:(NSString *)pluginKey {
648
667
NSAssert (self.pluginPublications[pluginKey] == nil , @" Duplicate plugin key: %@ " , pluginKey);
649
668
self.pluginPublications [pluginKey] = [NSNull null ];
650
- return [[[FlutterEngineRegistrar alloc ] initWithPlugin: pluginKey flutterEngine: self ] autorelease ];
669
+ FlutterEngineRegistrar* result = [[FlutterEngineRegistrar alloc ] initWithPlugin: pluginKey
670
+ flutterEngine: self ];
671
+ self.registrars [pluginKey] = result;
672
+ return [result autorelease ];
651
673
}
652
674
653
675
- (BOOL )hasPlugin:(NSString *)pluginKey {
@@ -686,20 +708,18 @@ - (void)setIsGpuDisabled:(BOOL)value {
686
708
687
709
@implementation FlutterEngineRegistrar {
688
710
NSString * _pluginKey;
689
- FlutterEngine* _flutterEngine;
690
711
}
691
712
692
713
- (instancetype )initWithPlugin : (NSString *)pluginKey flutterEngine : (FlutterEngine*)flutterEngine {
693
714
self = [super init ];
694
715
NSAssert (self, @" Super init cannot be nil" );
695
- _pluginKey = [pluginKey retain ];
696
- _flutterEngine = [ flutterEngine retain ] ;
716
+ _pluginKey = [pluginKey copy ];
717
+ _flutterEngine = flutterEngine;
697
718
return self;
698
719
}
699
720
700
721
- (void )dealloc {
701
722
[_pluginKey release ];
702
- [_flutterEngine release ];
703
723
[super dealloc ];
704
724
}
705
725
0 commit comments