@@ -124,6 +124,9 @@ static std::string* g_lockfile_path;
124
124
125
125
static Mutex* g_file_locker_mutex;
126
126
127
+ // Mutex for managing if FutureData is safe to use, or if it has been deleted.
128
+ static Mutex g_future_data_mutex;
129
+
127
130
static const char * kMessagingNotInitializedError = " Messaging not initialized." ;
128
131
129
132
static void HandlePendingSubscriptions ();
@@ -706,7 +709,10 @@ void Terminate() {
706
709
SetListener (nullptr );
707
710
ReleaseClasses (env);
708
711
util::Terminate (env);
709
- FutureData::Destroy ();
712
+ {
713
+ MutexLock lock (g_future_data_mutex);
714
+ FutureData::Destroy ();
715
+ }
710
716
}
711
717
712
718
// Start a service which will communicate with the Firebase Cloud Messaging
@@ -826,8 +832,13 @@ static void CompleteVoidCallback(JNIEnv* env, jobject result,
826
832
FutureHandle handle (future_id);
827
833
Error error =
828
834
(result_code == util::kFutureResultSuccess ) ? kErrorNone : kErrorUnknown ;
829
- ReferenceCountedFutureImpl* api = FutureData::Get ()->api ();
830
- api->Complete (handle, error, status_message);
835
+ MutexLock lock (g_future_data_mutex);
836
+ if (FutureData::Get () && FutureData::Get ()->api ()) {
837
+ ReferenceCountedFutureImpl* api = FutureData::Get ()->api ();
838
+ api->Complete (handle, error, status_message);
839
+ } else {
840
+ LogWarning (" Failed to complete Future as it was likely already deleted." );
841
+ }
831
842
if (result) env->DeleteLocalRef (result);
832
843
}
833
844
@@ -843,8 +854,13 @@ static void CompleteStringCallback(JNIEnv* env, jobject result,
843
854
SafeFutureHandle<std::string>* handle =
844
855
reinterpret_cast <SafeFutureHandle<std::string>*>(callback_data);
845
856
Error error = success ? kErrorNone : kErrorUnknown ;
846
- ReferenceCountedFutureImpl* api = FutureData::Get ()->api ();
847
- api->CompleteWithResult (*handle, error, status_message, result_value);
857
+ MutexLock lock (g_future_data_mutex);
858
+ if (FutureData::Get () && FutureData::Get ()->api ()) {
859
+ ReferenceCountedFutureImpl* api = FutureData::Get ()->api ();
860
+ api->CompleteWithResult (*handle, error, status_message, result_value);
861
+ } else {
862
+ LogWarning (" Failed to complete Future as it was likely already deleted." );
863
+ }
848
864
delete handle;
849
865
}
850
866
0 commit comments