Skip to content

Commit dc1b73a

Browse files
Googlera-maurice
authored andcommitted
Removed mutex usage in Admob destructor to signal the completion of Destroy. The use of Mutex in this case was not thread-safe. Replaced the mutex with semaphores and added an integration test for iOS and Android.
PiperOrigin-RevId: 279825051
1 parent 5e6926a commit dc1b73a

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

admob/src/android/banner_view_internal_android.cc

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include "admob/src/android/banner_view_internal_android.h"
18+
1719
#include <assert.h>
1820
#include <jni.h>
1921

@@ -23,13 +25,13 @@
2325
#include "admob/admob_resources.h"
2426
#include "admob/src/android/ad_request_converter.h"
2527
#include "admob/src/android/admob_android.h"
26-
#include "admob/src/android/banner_view_internal_android.h"
2728
#include "admob/src/common/admob_common.h"
2829
#include "admob/src/include/firebase/admob.h"
2930
#include "admob/src/include/firebase/admob/banner_view.h"
3031
#include "admob/src/include/firebase/admob/types.h"
3132
#include "app/src/assert.h"
3233
#include "app/src/mutex.h"
34+
#include "app/src/semaphore.h"
3335
#include "app/src/util_android.h"
3436

3537
namespace firebase {
@@ -59,21 +61,13 @@ BannerViewInternalAndroid::BannerViewInternalAndroid(BannerView* base)
5961
BannerViewInternalAndroid::~BannerViewInternalAndroid() {
6062
JNIEnv* env = ::firebase::admob::GetJNI();
6163

62-
// Destroy the banner view so all pending futures / callbacks complete.
63-
{
64-
Mutex mutex(Mutex::kModeNonRecursive);
65-
mutex.Acquire();
66-
Destroy().OnCompletion(
67-
[](const Future<void>&, void* mutex) {
68-
reinterpret_cast<Mutex*>(mutex)->Release();
69-
},
70-
&mutex);
71-
// Acquire a second Mutex lock to block until the Future for the last call
72-
// to Destroy() completes at which point the lambda function in OnCompletion
73-
// is called and the Mutex lock is released.
74-
mutex.Acquire();
75-
mutex.Release();
76-
}
64+
DestroyInternalData();
65+
66+
Semaphore semaphore(0);
67+
InvokeNullary(kBannerViewFnDestroyOnDelete, banner_view_helper::kDestroy)
68+
.OnCompletion([&semaphore](const Future<void>&) { semaphore.Post(); });
69+
semaphore.Wait();
70+
7771
env->DeleteGlobalRef(helper_);
7872
helper_ = nullptr;
7973
}
@@ -131,8 +125,7 @@ Future<void> BannerViewInternalAndroid::Resume() {
131125
}
132126

133127
Future<void> BannerViewInternalAndroid::Destroy() {
134-
// The bounding box is zeroed on destroy.
135-
bounding_box_ = {};
128+
DestroyInternalData();
136129
return InvokeNullary(kBannerViewFnDestroy, banner_view_helper::kDestroy);
137130
}
138131

@@ -214,6 +207,12 @@ Future<void> BannerViewInternalAndroid::InvokeNullary(
214207
return GetLastResult(fn);
215208
}
216209

210+
void BannerViewInternalAndroid::DestroyInternalData() {
211+
// The bounding box is zeroed on destroy.
212+
bounding_box_ = {};
213+
}
214+
215+
217216
} // namespace internal
218217
} // namespace admob
219218
} // namespace firebase

admob/src/android/banner_view_internal_android.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class BannerViewInternalAndroid : public BannerViewInternal {
7878
// the future callback pointer.
7979
Future<void> InvokeNullary(BannerViewFn fn,
8080
banner_view_helper::Method method);
81+
82+
// Cleans up any C++ side data before invoking the Android SDK to do the same.
83+
void DestroyInternalData();
8184
};
8285

8386
} // namespace internal

admob/src/common/banner_view_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum BannerViewFn {
3535
kBannerViewFnPause,
3636
kBannerViewFnResume,
3737
kBannerViewFnDestroy,
38+
kBannerViewFnDestroyOnDelete,
3839
kBannerViewFnMoveTo,
3940
kBannerViewFnCount
4041
};

0 commit comments

Comments
 (0)