Skip to content

Commit 491c111

Browse files
authored
Handle Future completion race condition (#846)
* Handle Future completion race condition * Make FreeData static, so it can be used if the future is deleted
1 parent 5714ceb commit 491c111

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

app/src/swig/future.i

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,18 @@ namespace firebase {
295295
// Free data structure allocated in SetOnCompletionCallback() and save
296296
// a reference to the current data structure if specified.
297297
private void SetCompletionData(System.IntPtr data) {
298-
ThrowIfDisposed();
299-
SWIG_FreeCompletionData(callbackData);
300-
callbackData = data;
298+
lock (FirebaseApp.disposeLock) {
299+
// The callback that was made could theoretically be triggered before this point,
300+
// which would Dispose this object. In that case, we want to free the data we
301+
// were given, since otherwise it would be leaked.
302+
if (swigCPtr.Handle == System.IntPtr.Zero) {
303+
SWIG_FreeCompletionData(data);
304+
} else {
305+
// Free the old data, before saving the new data for deletion
306+
SWIG_FreeCompletionData(callbackData);
307+
callbackData = data;
308+
}
309+
}
301310
}
302311

303312
// Handles the C++ callback, and calls the cached C# callback.
@@ -389,7 +398,7 @@ namespace firebase {
389398
}
390399

391400
// Deallocate data allocated for the completion callback.
392-
void SWIG_FreeCompletionData(void *data) {
401+
static void SWIG_FreeCompletionData(void *data) {
393402
delete reinterpret_cast<CSNAME##CallbackData*>(data);
394403
}
395404

0 commit comments

Comments
 (0)