Skip to content

Commit e806636

Browse files
authored
Bug fix - Ios memory leaks (#70)
* Fix bug on iOS where dispose() expects the wrong argument.
1 parent 025af72 commit e806636

File tree

6 files changed

+61
-5
lines changed

6 files changed

+61
-5
lines changed

packages/google_mobile_ads/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.11.0+3
22

3-
* Fixes an [Android crash](https://github.com/googleads/googleads-mobile-flutter/issues/46) when reusing Native and Banner Ad objects
3+
* Fixes an [Android crash](https://github.com/googleads/googleads-mobile-flutter/issues/46) when reusing Native and Banner Ad objects.
4+
* Fixes [iOS memory leaks](https://github.com/googleads/googleads-mobile-flutter/issues/69).
45

56
## 0.11.0+2
67

packages/google_mobile_ads/example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
2323
9EA7213725BB6517008D57E3 /* GoogleMobileAds.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9EA7213525BB6464008D57E3 /* GoogleMobileAds.xcframework */; };
2424
9EA7213825BB6530008D57E3 /* GoogleMobileAds.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9EA7213525BB6464008D57E3 /* GoogleMobileAds.xcframework */; };
25+
9EF1F00025F8351A0017A440 /* FLTGoogleMobileAdsPluginMethodCallsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 9EF1EFFF25F8351A0017A440 /* FLTGoogleMobileAdsPluginMethodCallsTest.m */; };
2526
FBE669D215209F1F44CEEB21 /* libPods-Runner.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 83F369F228D3A43519CEE308 /* libPods-Runner.a */; };
2627
/* End PBXBuildFile section */
2728

@@ -72,6 +73,7 @@
7273
97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
7374
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7475
9EA7213525BB6464008D57E3 /* GoogleMobileAds.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = GoogleMobileAds.xcframework; path = "Pods/Google-Mobile-Ads-SDK/Frameworks/GoogleMobileAdsFramework-Current/GoogleMobileAds.xcframework"; sourceTree = "<group>"; };
76+
9EF1EFFF25F8351A0017A440 /* FLTGoogleMobileAdsPluginMethodCallsTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FLTGoogleMobileAdsPluginMethodCallsTest.m; path = ../../../ios/Tests/FLTGoogleMobileAdsPluginMethodCallsTest.m; sourceTree = "<group>"; };
7577
CE82265EF05E2A9632B25E60 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
7678
F1C099CEFFFD5550FC343D00 /* libPods-google_mobile_ads_exampleTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-google_mobile_ads_exampleTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
7779
F74051031B69F8124E38352E /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
@@ -123,6 +125,7 @@
123125
8FB13CD3248F07B80011A72F /* google_mobile_ads_exampleTests */ = {
124126
isa = PBXGroup;
125127
children = (
128+
9EF1EFFF25F8351A0017A440 /* FLTGoogleMobileAdsPluginMethodCallsTest.m */,
126129
8FB13CE12498136E0011A72F /* FLTGoogleMobileAdsReaderWriterTest.m */,
127130
8FB13CDC248F07D90011A72F /* FLTGoogleMobileAdsTest.m */,
128131
8FB13CD6248F07B80011A72F /* Info.plist */,
@@ -391,6 +394,7 @@
391394
files = (
392395
8FB13CDD248F07D90011A72F /* FLTGoogleMobileAdsTest.m in Sources */,
393396
8FB13CE22498136E0011A72F /* FLTGoogleMobileAdsReaderWriterTest.m in Sources */,
397+
9EF1F00025F8351A0017A440 /* FLTGoogleMobileAdsPluginMethodCallsTest.m in Sources */,
394398
);
395399
runOnlyForDeploymentPostprocessing = 0;
396400
};

packages/google_mobile_ads/ios/Classes/FLTAdInstanceManager_Internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
- (id<FLTAd> _Nullable)adFor:(NSNumber *_Nonnull)adId;
2929
- (NSNumber *_Nullable)adIdFor:(id<FLTAd> _Nonnull)ad;
3030
- (void)loadAd:(id<FLTAd> _Nonnull)ad adId:(NSNumber *_Nonnull)adId;
31-
- (void)dispose:(id<FLTAd> _Nonnull)ad;
31+
- (void)dispose:(NSNumber *_Nonnull)adId;
3232
- (void)showAdWithID:(NSNumber *_Nonnull)adId;
3333
- (void)onAdLoaded:(id<FLTAd> _Nonnull)ad;
3434
- (void)onAdFailedToLoad:(id<FLTAd> _Nonnull)ad error:(FLTLoadAdError *_Nonnull)error;

packages/google_mobile_ads/ios/Classes/FLTAdInstanceManager_Internal.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ - (void)loadAd:(id<FLTAd> _Nonnull)ad adId:(NSNumber *_Nonnull)adId {
5858
[ad load];
5959
}
6060

61-
- (void)dispose:(id<FLTAd> _Nonnull)ad {
62-
[_ads removeObjectForKey:[self adIdFor:ad]];
61+
- (void)dispose:(NSNumber *_Nonnull)adId {
62+
[_ads removeObjectForKey:adId];
6363
}
6464

6565
- (void)showAdWithID:(NSNumber *_Nonnull)adId {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#import <OCMock/OCMock.h>
16+
#import <XCTest/XCTest.h>
17+
18+
#import "../Classes/FLTAdInstanceManager_Internal.h"
19+
#import "../Classes/FLTGoogleMobileAdsPlugin.h"
20+
21+
@interface FLTGoogleMobileAdsPluginMethodCallsTest : XCTestCase
22+
@end
23+
24+
@implementation FLTGoogleMobileAdsPluginMethodCallsTest {
25+
FLTGoogleMobileAdsPlugin *_fltGoogleMobileAdsPlugin;
26+
FLTAdInstanceManager *_mockAdInstanceManager;
27+
}
28+
29+
- (void)setUp {
30+
_mockAdInstanceManager = OCMClassMock([FLTAdInstanceManager class]);
31+
_fltGoogleMobileAdsPlugin = [[FLTGoogleMobileAdsPlugin alloc] init];
32+
[_fltGoogleMobileAdsPlugin setValue:_mockAdInstanceManager forKey:@"_manager"];
33+
}
34+
35+
- (void)testDisposeAd {
36+
FlutterMethodCall *methodCall = [FlutterMethodCall methodCallWithMethodName:@"disposeAd"
37+
arguments:@{@"adId" : @1}];
38+
__block bool resultInvoked = false;
39+
__block id _Nullable returnedResult;
40+
FlutterResult result = ^(id _Nullable result) {
41+
resultInvoked = true;
42+
returnedResult = result;
43+
};
44+
45+
[_fltGoogleMobileAdsPlugin handleMethodCall:methodCall result:result];
46+
47+
XCTAssertTrue(resultInvoked);
48+
XCTAssertNil(returnedResult);
49+
OCMVerify([_mockAdInstanceManager dispose:@1]);
50+
}
51+
@end

packages/google_mobile_ads/ios/Tests/FLTGoogleMobileAdsTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ - (void)testDisposeAd {
260260
OCMStub([mockBannerAd load]);
261261

262262
[_manager loadAd:bannerAd adId:@(1)];
263-
[_manager dispose:bannerAd];
263+
[_manager dispose:@(1)];
264264

265265
XCTAssertNil([_manager adFor:@(1)]);
266266
XCTAssertNil([_manager adIdFor:bannerAd]);

0 commit comments

Comments
 (0)