@@ -52,12 +52,30 @@ PlatformViewChannel::~PlatformViewChannel() {
52
52
}
53
53
54
54
void PlatformViewChannel::Dispose () {
55
+ ClearViewInstances ();
56
+ ClearViewFactories ();
57
+ }
58
+
59
+ void PlatformViewChannel::RemoveViewInstanceIfNeeded (int view_id) {
60
+ auto it = view_instances_.find (view_id);
61
+ if (view_id >= 0 && it != view_instances_.end ()) {
62
+ auto view_instance = it->second ;
63
+ view_instance->Dispose ();
64
+ delete view_instance;
65
+ view_instances_.erase (it);
66
+ }
67
+ }
68
+
69
+ void PlatformViewChannel::ClearViewInstances () {
55
70
// Clean-up view_instances_
56
71
for (auto const & [view_id, view_instance] : view_instances_) {
72
+ view_instance->Dispose ();
57
73
delete view_instance;
58
74
}
59
75
view_instances_.clear ();
76
+ }
60
77
78
+ void PlatformViewChannel::ClearViewFactories () {
61
79
// Clean-up view_factories_
62
80
for (auto const & [view_type, view_factory] : view_factories_) {
63
81
view_factory->Dispose ();
@@ -124,6 +142,7 @@ void PlatformViewChannel::HandleMethodCall(
124
142
FT_LOGI (
125
143
" PlatformViewChannel create viewType: %s id: %d width: %f height: %f " ,
126
144
view_type.c_str (), view_id, width, height);
145
+ RemoveViewInstanceIfNeeded (view_id);
127
146
128
147
EncodableMap values = std::get<EncodableMap>(arguments);
129
148
EncodableValue value = values[EncodableValue (" params" )];
@@ -170,6 +189,17 @@ void PlatformViewChannel::HandleMethodCall(
170
189
} else {
171
190
result->Error (" Can't find view id" );
172
191
}
192
+ } else if (method == " dispose" ) {
193
+ int view_id = -1 ;
194
+ if (std::holds_alternative<int >(arguments)) {
195
+ view_id = std::get<int >(arguments);
196
+ };
197
+ if (view_id < 0 || view_instances_.find (view_id) == view_instances_.end ()) {
198
+ result->Error (" Can't find view id" );
199
+ } else {
200
+ RemoveViewInstanceIfNeeded (view_id);
201
+ result->Success ();
202
+ }
173
203
} else {
174
204
int view_id = -1 ;
175
205
if (!GetValueFromEncodableMap (arguments, " id" , &view_id)) {
@@ -179,10 +209,7 @@ void PlatformViewChannel::HandleMethodCall(
179
209
180
210
auto it = view_instances_.find (view_id);
181
211
if (view_id >= 0 && it != view_instances_.end ()) {
182
- if (method == " dispose" ) {
183
- it->second ->Dispose ();
184
- result->Success ();
185
- } else if (method == " resize" ) {
212
+ if (method == " resize" ) {
186
213
double width = 0.0 , height = 0.0 ;
187
214
if (!GetValueFromEncodableMap (arguments, " width" , &width) ||
188
215
!GetValueFromEncodableMap (arguments, " height" , &height)) {
0 commit comments