[HB-6075] Improve IsValid logic for Ad Disposal #152
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Current implementation of IsValid logic worked perfectly fine for iOS for the following scenarios:
For Android the following scenarios are the case:
Although the approaches above were tested during the development of the fullscreen API changes and all scenarios were working fine. It seems like official release of 4.3.0 might have had threading changes that affect how Unity retains the
AndroidJavaObject
reference.As such, the main problem is that the
AndroidJavaObject
reference gets disposed from the Unity side of things before we the finalizer for the C# objects is called. This means that whenever we try to call Invalidate orDestroy
, the action is performed on an invalid pointer thus causing a crash. This happens into a different thread from the Unity main thread, when calling such code from the Unity main thread, we get a null reference exception.The solution implemented was to create an
AdStore
, similar to how we do in iOS. ThisAdStore
lives purely on the Native layer, there we keep a reference to our Android ads. Whenever an Ad is disposed, either manually or automatically by theGC
, we call the method to remove such ad from theAdStore
, which on itself calls the invalidate/destroy methods accordingly properly disposing of the native Android ad.By implementing this solution we tackle the issue above, but we also provide an improvement on the ad integration experience for the Chartboost Mediation Unity SDK. Currently, if users do not dispose of Ads manually, they will remain on the application, which can end up in edge scenarios where a banner view, or a fullscreen ad leaks through the application lifetime. This fix makes sure that if an Ad is not manually disposed, it will wait until it is GC, and then call the appropriate methods.
It is important to notice that even though this solution makes it so the GC properly disposed of the native ads, this is not the most optimized integration. The most optimized integration will always be to properly manage and dispose the ad instances when no longer needed.
Finally, we have added a warning for when ads get disposed by the GC. It is as follows:
Similar warnings have been added for all kind of placements.