AppLovin / AppLovin-MAX-Unity-Plugin

129 stars 35 forks source link

[Android] Throws NullReferenceException when I try to invoke a function #401

Closed cihadturhan closed 1 month ago

cihadturhan commented 4 months ago

MAX Plugin Version

6.5.2

Unity Version

2022.3.22f1

Device/Platform Info

Android 10 - 11

Current Behavior

The code snippet below throws NullReferenceException when the it fails to load a rewarded ad.

  private void OnLoad(){
      MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdLoadFailedEvent;
  }
  // ...

  private void OnRewardedAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
  {
      // Rewarded ad failed to load 
      // AppLovin recommends that you retry with exponentially higher delays, up to a maximum delay (in this case 64 seconds).

      retryAttempt++;
      double retryDelay = Math.Pow(2, Math.Min(6, retryAttempt));

      Invoke(nameof(LoadRewardedAd), (float)retryDelay); // <<< This line throws error
  }

  private void LoadRewardedAd()
  {
      MaxSdk.LoadRewardedAd(adUnitId);
  }

Expected Behavior

I'd expect Invoke function to be called without issues but it throws NullReferenceException for the called function name.

2024/07/23 12:39:00.221 14308 14422 Error Unity Error [AppLovin MAX] Caught exception in publisher event: OnInterstitialLoadFailedEvent, exception: System.NullReferenceException: Object reference not set to an instance of an object.
2024/07/23 12:39:00.221 14308 14422 Error Unity   at IsAdController.OnInterstitialLoadFailedEvent (System.String adUnitId, MaxSdkBase+ErrorInfo errorInfo) [0x00000] in <00000000000000000000000000000000>:0 
2024/07/23 12:39:00.221 14308 14422 Error Unity   at AppLovinMax.Internal.MaxEventExecutor.Update () [0x00000] in <00000000000000000000000000000000>:0 

How to Reproduce

Additional Info

No response

santoshbagadi commented 4 months ago

Hi @cihadturhan , the NullReferenceException seems to be happening in OnInterstitialLoadFailedEvent. Could you please check the implementation of that method instead of OnRewardedAdLoadFailedEvent.

cihadturhan-st commented 4 months ago

Hi @cihadturhan , the NullReferenceException seems to be happening in OnInterstitialLoadFailedEvent. Could you please check the implementation of that method instead of OnRewardedAdLoadFailedEvent.

Hey, that’s my bad when posting here. But the issue is same for both OnRewardedAdLoadFailedEvent

AppLovin callbacks cannot execute Invoke on android. I think this callbacks are called in threads other than main. Is it correct?

In my case, I converted function to static and called it directly instead of Invoke. So it’s resolved but this problem is still there

JonathanLiuApp commented 4 months ago

@cihadturhan Yeah most of the callbacks are called in background threads unless you specify otherwise. Could you try setting MaxSdkBase.InvokeEventsOnUnityMainThread = true; and see if that helps?

cihadturhan-st commented 4 months ago

Could you try setting MaxSdkBase.InvokeEventsOnUnityMainThread = true;

Wow is there such a variable? I’ll try that in the next build. I sometimes see random crashes maybe that’s the reason.

Is there any drawbacks of running events on the main thread (ie freezing the ui or network requests)?

JonathanLiuApp commented 4 months ago

It would have the same drawbacks as putting the event in a Unity event function like Start() or Update(). Depending on the code in the callback there could be differences in performance, but for most cases there would likely be no difference.

cihadturhan commented 4 months ago

Could you try setting MaxSdkBase.InvokeEventsOnUnityMainThread = true; and see if that helps?

Hi Jonathan, kinda related to this issue so I wanted to ask here. I saw some crashes and some devs reported it's because of the crashes in related to callbacks from Admob are not being called in the main thread.

Example: https://github.com/googleads/googleads-mobile-unity/issues/2854#issuecomment-1914807577

I wonder when I add MaxSdkBase.InvokeEventsOnUnityMainThread, does it call MobileAds.RaiseAdEventsOnUnityMainThread = true for Admob and other libraries too?

JonathanLiuApp commented 4 months ago

MaxSdkBase.InvokeEventsOnUnityMainThread only ensures that our own callbacks from MaxSdkCallbacks.cs are invoked on the main thread. It doesn't set MobileAds.RaiseAdEventsOnUnityMainThread or similar in other libraries so it shouldn't be causing any crashes related to Admob callbacks.

cihadturhan commented 4 months ago

MaxSdkBase.InvokeEventsOnUnityMainThread only ensures that our own callbacks from MaxSdkCallbacks.cs are invoked on the main thread. It doesn't set MobileAds.RaiseAdEventsOnUnityMainThread or similar in other libraries so it shouldn't be causing any crashes related to Admob callbacks.

Thanks for the response. I'm trying it now. I'm guessing it should be set before initialization of the SDK? I'm assuming that even the other libraries call MaxSdkBase in other thread, the statement InvokeEventsOnUnityMainThread will eventually call unity callbacks in the main thread.

JonathanLiuApp commented 4 months ago

It is best to set it prior to initialization, otherwise the onSdkInitializedEvent might not be pushed to the main thread.

cihadturhan commented 1 month ago

Closing this issue as I kinda resolved it. Solution: never wait for a callback, instead set a boolean variable and check if the value is changed in update() loop.