@@ -52,12 +52,30 @@ PlatformViewChannel::~PlatformViewChannel() {
5252}
5353
5454void 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 () {
5570 // Clean-up view_instances_
5671 for (auto const & [view_id, view_instance] : view_instances_) {
72+ view_instance->Dispose ();
5773 delete view_instance;
5874 }
5975 view_instances_.clear ();
76+ }
6077
78+ void PlatformViewChannel::ClearViewFactories () {
6179 // Clean-up view_factories_
6280 for (auto const & [view_type, view_factory] : view_factories_) {
6381 view_factory->Dispose ();
@@ -124,6 +142,7 @@ void PlatformViewChannel::HandleMethodCall(
124142 FT_LOGI (
125143 " PlatformViewChannel create viewType: %s id: %d width: %f height: %f " ,
126144 view_type.c_str (), view_id, width, height);
145+ RemoveViewInstanceIfNeeded (view_id);
127146
128147 EncodableMap values = std::get<EncodableMap>(arguments);
129148 EncodableValue value = values[EncodableValue (" params" )];
@@ -170,6 +189,17 @@ void PlatformViewChannel::HandleMethodCall(
170189 } else {
171190 result->Error (" Can't find view id" );
172191 }
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+ }
173203 } else {
174204 int view_id = -1 ;
175205 if (!GetValueFromEncodableMap (arguments, " id" , &view_id)) {
@@ -179,10 +209,7 @@ void PlatformViewChannel::HandleMethodCall(
179209
180210 auto it = view_instances_.find (view_id);
181211 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" ) {
186213 double width = 0.0 , height = 0.0 ;
187214 if (!GetValueFromEncodableMap (arguments, " width" , &width) ||
188215 !GetValueFromEncodableMap (arguments, " height" , &height)) {
0 commit comments