afriscic / BarcodeScanning.Native.Maui

Barcode scanning library for .NET MAUI
https://www.nuget.org/packages/BarcodeScanning.Native.Maui
MIT License
162 stars 31 forks source link

Memory Leak in CameraView (on iOS) #45

Closed marco-skizza closed 5 months ago

marco-skizza commented 5 months ago

Hi afriscic

Many thanks for your work on this project lately.

Unfortunately, since version 1.4.0 the CameraView started to leak again. I.e. the page containing a CameraView never gets garbage collected anymore.

        <scanner:CameraView
            x:Name="MyCameraView"
            OnDetectionFinished="MyCameraView_OnOnDetectionFinished"
            AimMode="True"
            CaptureQuality="Highest" />

I have a sample project that demonstrates that problem: https://github.com/marco-skizza/BarcodeScanningNativeMauiLeak

Steps to Reproduce:

This seems to be a problem with iOS as well as Android.

afriscic commented 5 months ago

Hello.

I tried your code, at least on Android. The problem is that in your example MyCameraView.Handler?.DisconnectHandler(); never gets called. When I added in ScannerLeakPage.xaml:

...Title="ScannerLeakPage"
   Unloaded="ContentPage_Unloaded">

and in ScannerLeakPage.xaml.cs:

private void ContentPage_Unloaded(object sender, EventArgs e)
{
    MyCameraView.Handler?.DisconnectHandler();
}

then DisconnectHandler() and ~ScannerLeakPage() both get called and memory footprint stays consistent.

I don't know why, at least on Android emulator, it takes 2-3 page openings and closings so that ~ScannerLeakPage() gets called but after that it is called consistently.

marco-skizza commented 5 months ago

Sorry for that! I forgot that I have the specific dispose code for iOS in place. So I guess it's an iOS issue only...

P.S.: Yeah, the Garbage Collector is not very predictable. For me it also takes several forth and back to observe it correctly.

afriscic commented 5 months ago

Can you please report back the last version that you can confirm that got garage collected normally on iOS

marco-skizza commented 5 months ago

I'm now using version 1.3.1 from NuGet. This seems to be the last version where the control gets Garbage Collected.

afriscic commented 5 months ago

Hopefully it is fixed in 1.4.2. I have tested your example on Android and macOS and ~ScannerLeakPage() gets triggered. Please, if you can test on iOS and report back.

marco-skizza commented 5 months ago

Yes, it works now. Many thanks!

marco-skizza commented 5 months ago

Fixed in version 1.4.2