GestureSample icon indicating copy to clipboard operation
GestureSample copied to clipboard

"Warning: observer object was not disposed manually with Dispose()" Messages Often Logged

Open appsure opened this issue 2 years ago • 6 comments

Hi, I've finally been able to create a repro on the latest 7 .net Maui. If you switch from a Mr.Gestures.Border to a Maui Border, the messages don't appear. Please let me know if you need any other info. Really need this resolved before moving out of our beta. Thanks.

MrGestureDisposeRepro.zip

appsure avatar Jul 25 '23 15:07 appsure

On which platform do you get those warnings? I tried Windows and Android (after adding a using and removing the license key), but didn't get any warnings. For iOS VS4Win claimed that I needed to accept an updated Apple developer Program License Agreement. I did that, but VS still thinks I didn't. I don't know if that takes them a while.

MichaelRumpler avatar Jul 26 '23 09:07 MichaelRumpler

I'm seeing this on iOS 16.4, simulator & physical device. I've seen it on previous iOS versions as well.

appsure avatar Jul 26 '23 10:07 appsure

I could finally reproduce the problem. I get 24 of these warnings when the app starts (page is shown):

2023-07-26 13:38:54.774 MrGestureDisposeRepro[3281:978917] Warning: observer object was not disposed manually with Dispose()

But there are only 10 MR.Gestures.Border objects on the page.

When I searched for the warning, I found this SO question from 2015. They got exactly the same warning and "observer" there meant the iOS observer and no managed object at all.

MR.Gestures does not use any iOS observers, so this warning should not be caused by my code.

I also copied the page from your repro project to the GestureSample. The only difference is, that I don't use the CommunityToolkit there. There are no warnings when I open the page in the GestureSample. When I debugged it, I could see that the MR.Gestures objects are all disposed correctly when the page is closed.

So I don't know why this warning is logged with a MR.Gestures.Border but not with a standard MAUI Border. But as I don't use any iOS observers and the warning already comes when the page opens (long before I can dispose anything), it doesn't seem likely that this is caused by MR.Gestures.

As a last test I removed the CommunityToolkit from your repro project. But the warning still persists so this is not causing the problem.

So I would search in the MAUI source for where they use any iOS observers to see what could cause this. With that information you could open an issue there. If you find a definite problem, you could even fix it yourself and create a PR. This would definitely speed up a fix.

MichaelRumpler avatar Jul 26 '23 13:07 MichaelRumpler

The message is logged in https://github.com/xamarin/xamarin-macios/blob/main/src/Foundation/NSObject2.cs#L1092

I don't know if one can debug this somehow. It would be interesting to know, which object exactly was not disposed and where that object was created.

MichaelRumpler avatar Jul 26 '23 15:07 MichaelRumpler

Interesting - is MAUI using this codebase still for the Foundation classes that is still under Xamarin?

appsure avatar Jul 27 '23 11:07 appsure

Sure.

They renamed the dll to Microsoft.iOS.dll now. You can find it in your bin directory. When you open that dll with e.g. dotPeek, you can find the code in Foundation/NSObject/Observer/Dispose(bool disposing).

Unfortunately I don't know how to debug that. But please let me know what you find out.

MichaelRumpler avatar Jul 27 '23 11:07 MichaelRumpler