Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(firestore, windows): improve memory management #12575

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ std::map<std::string,
cloud_firestore_windows::CloudFirestorePlugin::transaction_handlers_;
std::map<std::string, std::shared_ptr<firebase::firestore::Transaction>>
cloud_firestore_windows::CloudFirestorePlugin::transactions_;
std::map<std::string, firebase::firestore::Firestore*>
std::map<std::string, std::unique_ptr<firebase::firestore::Firestore>>
cloud_firestore_windows::CloudFirestorePlugin::firestoreInstances_;

std::string RegisterEventChannelWithUUID(
Expand Down Expand Up @@ -185,7 +185,8 @@ CloudFirestorePlugin::~CloudFirestorePlugin() {}
Firestore* GetFirestoreFromPigeon(const FirestorePigeonFirebaseApp& pigeonApp) {
if (CloudFirestorePlugin::firestoreInstances_.find(pigeonApp.app_name()) !=
CloudFirestorePlugin::firestoreInstances_.end()) {
return CloudFirestorePlugin::firestoreInstances_[pigeonApp.app_name()];
return CloudFirestorePlugin::firestoreInstances_[pigeonApp.app_name()]
.get();
}

App* app = App::GetInstance(pigeonApp.app_name().c_str());
Expand Down Expand Up @@ -222,7 +223,8 @@ Firestore* GetFirestoreFromPigeon(const FirestorePigeonFirebaseApp& pigeonApp) {

firestore->set_settings(settings);

CloudFirestorePlugin::firestoreInstances_[pigeonApp.app_name()] = firestore;
CloudFirestorePlugin::firestoreInstances_[pigeonApp.app_name()] =
std::unique_ptr<firebase::firestore::Firestore>(firestore);

return firestore;
}
Expand Down Expand Up @@ -538,6 +540,7 @@ class LoadBundleStreamHandler
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events)
override {
events_ = std::move(events);
events.reset();
firestore_->LoadBundle(
bundle_, [this](const LoadBundleTaskProgress& progress) {
flutter::EncodableMap map;
Expand Down Expand Up @@ -755,6 +758,7 @@ class SnapshotInSyncStreamHandler
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events)
override {
events_ = std::move(events);
events.reset();
// We do this to bind the event to the main channel
auto boundSendEvent =
std::bind(&SnapshotInSyncStreamHandler::SendEvent, this);
Expand Down Expand Up @@ -830,6 +834,7 @@ class TransactionStreamHandler
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events)
override {
events_ = std::move(events);
events.reset();
TransactionOptions options;
options.set_max_attempts(maxAttempts_);

Expand Down Expand Up @@ -1549,6 +1554,7 @@ class QuerySnapshotStreamHandler
: MetadataChanges::kExclude;

events_ = std::move(events);
events.reset();

listener_ = query_->AddSnapshotListener(
metadataChanges,
Expand Down Expand Up @@ -1654,6 +1660,7 @@ class DocumentSnapshotStreamHandler
: MetadataChanges::kExclude;

events_ = std::move(events);
events.reset();

listener_ = reference_->AddSnapshotListener(
metadataChanges,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class CloudFirestorePlugin : public flutter::Plugin,
static std::map<std::string,
std::shared_ptr<firebase::firestore::Transaction>>
transactions_;
static std::map<std::string, firebase::firestore::Firestore*>
static std::map<std::string, std::unique_ptr<firebase::firestore::Firestore>>
firestoreInstances_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,16 @@ cloud_firestore_windows::FirestoreCodec::ReadValueOfType(
if (CloudFirestorePlugin::firestoreInstances_.find(appName) !=
CloudFirestorePlugin::firestoreInstances_.end()) {
return CustomEncodableValue(
CloudFirestorePlugin::firestoreInstances_[appName]);
CloudFirestorePlugin::firestoreInstances_[appName].get());
}

firebase::App* app = firebase::App::GetInstance(appName.c_str());

Firestore* firestore = Firestore::GetInstance(app);
firestore->set_settings(settings);

CloudFirestorePlugin::firestoreInstances_[appName] = firestore;
CloudFirestorePlugin::firestoreInstances_[appName] =
std::unique_ptr<firebase::firestore::Firestore>(firestore);

return CustomEncodableValue(firestore);
}
Expand Down
Loading