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

Conversation

@iskakaushik
Copy link
Contributor

Prior to this change FlutterPlatformViewsController was passed around as a raw pointer. This change makes it so that this is instead shared as a shared_ptr. This also makes the current unique_ptr hold in FlutterEngine.mm to be a shared one.

#pragma mark - Platform views

- (flutter::FlutterPlatformViewsController*)platformViewsController {
- (std::shared_ptr<flutter::FlutterPlatformViewsController>)platformViewsController {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this getting called at least once per frame? If so it may be desirable to pass back a const reference to allow any copying of the shared_ptr.

FlutterViewController* view_controller_;
PlatformViewIOS* platform_view_;
FlutterPlatformViewsController* platform_views_controller_;
const std::shared_ptr<FlutterPlatformViewsController> platform_views_controller_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another big win here, nice.


private:
FlutterPlatformViewsController* platform_views_controller_;
const std::shared_ptr<FlutterPlatformViewsController>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Member

@gaaclarke gaaclarke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a nit that I'd avoid copying the shared_ptr so much by using const references like i've mentioned in your other PR. Especially if this stuff is happening at least once a frame.

Also, a warning, you might be introducing a retain cycle. I'd run on device to make sure that FlutterPlatformViewsController gets deleted when you delete an engine. I don't know if FlutterPlatformViewsController, AccessibilityBridge, FlutterEngine, ExternalViewEmbedder form a cycle. But it's complicated enough that it might be.

@iskakaushik
Copy link
Contributor Author

I tried making it a const reference initially, but then ran into a problem where we pass in nullptr all the way down to the ios_external_view_embedder.mm. Also the platformViewsController object is created once per engine and all the components that use it also are created in engine or shell initialization.

About the retain cycle, good point, I will take a look and see if it's a problem.

@gaaclarke
Copy link
Member

I tried making it a const reference initially, but then ran into a problem where we pass in nullptr all the way down to the ios_external_view_embedder.mm

That isn't a problem. There is an implicit constructor for shared_ptr that will create an rvalue whose reference will get passed on the stack, try this out:

#include <memory>
#include <iostream>

namespace {
  void foo(const std::shared_ptr<int>& ptr ) {
    std::cout << ptr.get() << std::endl;
  }
}

int main() {
  foo(nullptr);
}

@iskakaushik
Copy link
Contributor Author

@gaaclarke makes sense. I've moved them to be const references.

@iskakaushik
Copy link
Contributor Author

I am still investigating to see if there is a retain cycle, and also looking to see if platformViewsController on FlutterEngine can be called multiple times per frame.

@iskakaushik
Copy link
Contributor Author

platformViewsController is not called multiple times per frame but I saw no harm in making it return a reference instead, so changed it. Also I ran the app and killed the engine and saw that the platformViewsController indeed got destroyed.

@iskakaushik iskakaushik merged commit a598f23 into flutter:master Oct 26, 2020
chaselatta pushed a commit to chaselatta/engine that referenced this pull request Nov 30, 2020
…r#22082)

* Convert FlutterPlatformViewsController to smart pointer

* have a const reference of platform views controller

* change more stuff to references
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants