Skip to content

Commit f799d80

Browse files
chkuang-ga-maurice
authored andcommitted
Change Functions to be more robust if App is deleted out from under them.
If App is deleted, invalidate the internal pointers (e.g. FunctionsInternal) rather than the outer pointers (Functions), so that later code can still delete the outer pointer. PiperOrigin-RevId: 238808270
1 parent 94580e9 commit f799d80

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

functions/src/common/functions.cc

+12-3
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,18 @@ Functions::Functions(::firebase::App* app, const char *region) {
115115
static_cast<int>(reinterpret_cast<intptr_t>(functions)),
116116
static_cast<int>(reinterpret_cast<intptr_t>(
117117
functions->app())));
118-
delete functions;
118+
functions->DeleteInternal();
119119
});
120120
}
121121
}
122122

123-
Functions::~Functions() {
123+
Functions::~Functions() { DeleteInternal(); }
124+
125+
void Functions::DeleteInternal() {
124126
MutexLock lock(g_functions_lock);
127+
128+
if (!internal_) return;
129+
125130
CleanupNotifier* app_notifier = CleanupNotifier::FindByOwner(app());
126131
assert(app_notifier);
127132
app_notifier->UnregisterObject(this);
@@ -140,13 +145,17 @@ Functions::~Functions() {
140145
}
141146
}
142147

143-
::firebase::App* Functions::app() { return internal_->app(); }
148+
::firebase::App* Functions::app() {
149+
return internal_ ? internal_->app() : nullptr;
150+
}
144151

145152
HttpsCallableReference Functions::GetHttpsCallable(const char* name) const {
153+
if (!internal_) return HttpsCallableReference();
146154
return HttpsCallableReference(internal_->GetHttpsCallable(name));
147155
}
148156

149157
void Functions::UseFunctionsEmulator(const char* origin) {
158+
if (!internal_) return;
150159
internal_->UseFunctionsEmulator(origin);
151160
}
152161

functions/src/include/firebase/functions.h

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ class Functions {
9999
Functions(const Functions& src);
100100
Functions& operator=(const Functions& src);
101101

102+
// Delete the internal_ data.
103+
void DeleteInternal();
104+
102105
internal::FunctionsInternal* internal_;
103106
/// @endcond
104107
};

0 commit comments

Comments
 (0)