maui icon indicating copy to clipboard operation
maui copied to clipboard

System.ObjectDisposedException: ObjectDisposed_Generic ObjectDisposed_ObjectName_Name, Android.OS.PowerManager

Open mattgagnon2 opened this issue 1 year ago • 5 comments

Description

When running our application without a charging cable plugged in, the application will crash at any point below 50% battery charge. There is currently no reference the the Battery object. If the application is running and the charging cable is plugged in, the same crash occurs.

Java.Interop.JniPeerMembers.AssertSelf(IJavaPeerable ) Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualBooleanMethod(String , IJavaPeerable , JniArgumentValue* ) Android.OS.PowerManager.get_IsPowerSaveMode() Microsoft.Maui.Devices.BatteryImplementation.get_EnergySaverStatus() Microsoft.Maui.Devices.BatteryImplementation.OnEnergySaverChanged() Microsoft.Maui.Devices.EnergySaverBroadcastReceiver.OnReceive(Context , Intent ) Android.Content.BroadcastReceiver.n_OnReceive_Landroid_content_Context_Landroid_content_Intent_(IntPtr jnienv, IntPtr native__this, IntPtr native_context, IntPtr native_intent) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLL_V(_JniMarshal_PPLL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1)

I believe that a third party component, Shiny Jobs, is disposing the the PowerManager after the battery implementation has retrieved it.

Steps to Reproduce

  1. Start the app
  2. Unplug the usb cable
  3. Wait for the battery info to change

Link to public reproduction project repository

ObjectDisposedSample

Version with bug

8.0.21

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

Keep the usb cable plugged in

Relevant log output

No response

mattgagnon2 avatar May 03 '24 20:05 mattgagnon2

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

github-actions[bot] avatar May 03 '24 20:05 github-actions[bot]

Updated with repo steps and sample repo

mattgagnon2 avatar May 07 '24 15:05 mattgagnon2

@mattgagnon2 No. I'm disposing of my attachment to a native service. That does not effect others pulling in that instance.

aritchie avatar May 07 '24 16:05 aritchie

I am just trying to find a solution. You can confirm with the sample app attached and repo steps that the exception is thrown when the trying to get the EnergySaverStatus or when the OnEnergySaverStatusChanged event is run, and if you comment out the line 31 in MauiProgram that registers the job to run, the exception doesn't happen. Shiny has a using statement of using var pm = this.platform.GetSystemService<Android.OS.PowerManager>(Context.PowerService); that I thought might be disposing the PowerManager and thus throwing the exception.

mattgagnon2 avatar May 07 '24 16:05 mattgagnon2

Verified this issue with Visual Studio 17.10 Preview 7.0 (8.0.21). Can repro on android platform with sample project. image

Zhanglirong-Winnie avatar May 11 '24 05:05 Zhanglirong-Winnie

This is now fixed by https://github.com/dotnet/maui/pull/22281

mattleibow avatar Jun 03 '24 14:06 mattleibow