Skip to content

Commit

Permalink
fix(firestore, windows): improve memory management (firebase#12575)
Browse files Browse the repository at this point in the history
* fix(firestore, windows): improve memory managment

* format
  • Loading branch information
Lyokone authored and LocLt-Mobile committed Apr 5, 2024
1 parent b47ecda commit 94a32c4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
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

0 comments on commit 94a32c4

Please sign in to comment.