Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

[CollectionView, Android] DisposedObjectException thrown when adding elements to ItemsSource after having disposed CollectionView containing EmptyView #6427

Closed
jsuvanto opened this issue Jun 6, 2019 · 13 comments
Assignees
Labels
a/collectionview e/2 🕑 2 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/Android t/bug 🐛

Comments

@jsuvanto
Copy link

jsuvanto commented Jun 6, 2019

Description

Adding elements to the ItemsSource of a CollectionView, that has EmptyView set, throws an exception. This exception cannot be caught calling the Add() function inside a try-catch. A workaround is not to set the EmptyView.

Steps to Reproduce

  1. Create a new Xamarin.Forms project
  2. Create a page containing a CollectionView that has ItemsSource and EmptyView set
  3. Open and close the page with Navigation.PushAsync() and .PopAsync()
  4. Add an element to the ItemsSource collection

Expected Behavior

Element is added

Actual Behavior

An ObjectDisposedException is thrown:

Unhandled Exception:

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Xamarin.Forms.Platform.Android.CollectionViewRenderer'.

=================================================================
	Native Crash Reporting
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
	Basic Fault Adddress Reporting
=================================================================

space (region space) (deleted)Memory around native instruction pointer (0x78e3800678):
0x78e3800668  00 1c 40 b9 c0 03 5f d6 fd 7b bf a9 fd 03 00 91  ..@..._..{......
0x78e3800678  08 20 40 b9 a8 00 c8 37 88 01 e0 37 00 00 40 f9  . @....7...7..@.
0x78e3800688  fd 7b c1 a8 c0 03 5f d6 60 08 00 b0 01 0a 00 f0  .{...._.`.......
0x78e3800698  03 0a 00 f0 00 24 06 91 21 80 08 91 63 8c 0a 91  .....$..!...c...

No native Android stacktrace (see debuggerd output).

=================================================================
	Managed Stacktrace:
=================================================================

at <unknown> <0xffffffff>
at Java.Interop.NativeMethods:java_interop_jnienv_call_nonvirtual_void_method_a <0x00007>
at InstanceMethods:CallNonvirtualVoidMethod <0x0031b>
at JniInstanceMethods:InvokeNonvirtualVoidMethod <0x0016b>
at Adapter:NotifyItemInserted <0x0017f>
at Xamarin.Forms.Platform.Android.ObservableItemsSource:Add <0x0026f>
at Xamarin.Forms.Platform.Android.ObservableItemsSource:CollectionChanged <0x000d7>
at System.Collections.ObjectModel.ObservableCollection`1:OnCollectionChanged <0x00103>
at System.Collections.ObjectModel.ObservableCollection`1:OnCollectionChanged <0x000cb>
at System.Collections.ObjectModel.ObservableCollection`1:InsertItem <0x000d3>
at System.Collections.ObjectModel.Collection`1:Add <0x00183>
at <AddDeviceAsync>d__25:MoveNext <0x005db>
at MoveNextRunner:InvokeMoveNext <0x000f3>
at System.Threading.ExecutionContext:RunInternal <0x0040f>
at System.Threading.ExecutionContext:Run <0x0006b>
at MoveNextRunner:Run <0x00187>
at System.Threading.Tasks.AwaitTaskContinuation:InvokeAction <0x00097>
at System.Threading.Tasks.AwaitTaskContinuation:RunCallback <0x0010b>
at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation:Run <0x000fb>
at System.Threading.Tasks.Task:FinishContinuations <0x005f7>
at System.Threading.Tasks.Task:FinishStageThree <0x00173>
at System.Threading.Tasks.Task`1:TrySetResult <0x00213>
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult <0x0021f>
at <GetDeviceAsync>d__11:MoveNext <0x00a37>
at MoveNextRunner:InvokeMoveNext <0x000f3>
at System.Threading.ExecutionContext:RunInternal <0x0040f>
at System.Threading.ExecutionContext:Run <0x0006b>
at MoveNextRunner:Run <0x00187>
at <>c:<.cctor>b__7_0 <0x0009b>
at <>c__DisplayClass2_0:<Post>b__0 <0x00093>
at RunnableImplementor:Run <0x000bb>
at Java.Lang.IRunnableInvoker:n_Run <0x000c3>
at Android.Runtime.DynamicMethodNameCounter:37 <0x0001f>
at Android.Runtime.DynamicMethodNameCounter:37 <0x000c7>
=================================================================

Basic Information

  • Version with issue: 4.0.0.394984-pre10
  • Last known good version: N/A
  • IDE: Visual Studio 2019 (16.1.1)
  • Platform Target Frameworks:
    • Android: 8.1
  • Android Support Library Version: 28.0.0.1
  • Affected Devices: Android 8.1
@jsuvanto jsuvanto added s/unverified New report that has yet to be verified t/bug 🐛 labels Jun 6, 2019
@kvpt
Copy link
Contributor

kvpt commented Jun 6, 2019

From the code it's because the ItemsViewAdapter is never disposed in the dispose method of the ItemsViewRenderer. I don't know if it's intended or not.

@kvpt
Copy link
Contributor

kvpt commented Jun 6, 2019

Can you provide a sample with the issue please ?
I tried to setup what you mention in your description to confirm the cause but it don't reproduce the issue.

@hartez hartez self-assigned this Jun 6, 2019
@samhouts samhouts added a/collectionview i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/Android labels Jun 6, 2019
@samhouts
Copy link
Contributor

samhouts commented Jun 6, 2019

@jsuvanto Can you please attach a small project that demonstrates this issue? Thanks!

@samhouts samhouts added s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. labels Jun 6, 2019
@jsuvanto
Copy link
Author

jsuvanto commented Jun 7, 2019

I'm not sure if behaviour somehow depends on whether the EmptyView was specified in XAML or Code-behind. I haven't had the time to test this more thoroughly. I cannot provide a project just yet, but here's some excerpts of the code I used:

Page.xaml:

<CollectionView EmptyView="No devices"
                ItemsSource="{Binding Path=Devices, Source={x:Static local:App.MainViewModel}}">
    <CollectionView.ItemsLayout>
        <ListItemsLayout SnapPointsAlignment="Center" SnapPointsType="Mandatory">
            <x:Arguments>
                <ItemsLayoutOrientation>Horizontal</ItemsLayoutOrientation>
            </x:Arguments>
        </ListItemsLayout>
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <ContentView>
                <!-- content -->
            </ContentView>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

MainViewModel.cs:

public class MainViewModel : INotifyPropertyChanged
{
    public ObservableCollection<MyDevice> Devices { get; }

    public MainViewModel()
    {
        Devices = new ObservableCollection<MyDevice>();
    }

    public AddDevice(MyDevice device)
    {
        try
        {
            Devices.Add(device);
        }
        catch (Exception)
        {
            // never caught
        }
    }
}

I'll try to provide a project that reproduces the behaviour as soon as I can. I hope these snippets help you meanwhile.

@samhouts samhouts removed s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. labels Jun 10, 2019
@jfversluis
Copy link
Member

@jsuvanto tried to make something out of your snippets together with your reproduction steps but I'm not quite sure what you're trying to do. See my (very ugly) code here.

I got a bit confused with this bit:

Open and close the page with Navigation.PushAsync() and .PopAsync()

You are trying to add something to a collection on a page that has been closed?

I also see @kvpt already has some fix for (also) this in the making?

@jsuvanto
Copy link
Author

@jfversluis My application has a main page with buttons that open a QR scanner and a device list page, which contains the CollectionView. Scanning a QR code adds a device to a list of devices, which serves as the CollectionView's item source. The device list page is opened with Navigation.PushAsync() and it is closed with Navigation.PopAsync().

If I open the device list page before any devices are added, return to main page and scan a QR code, the exception will be thrown. So yes, I am adding the new item to the item source while the page containing the CollectionView is not open.

The QR scanner has been ruled out as the source of the problem. Using any method to add a new device to the item source should throw the exception.

Unfortunately I am not authorised to publish the project where I first encountered this bug, since it is work-related code, and I haven't found myself the time to reproduce the bug.

I appreciate your and @kvpt's help. If you have any more questions about this bug, please feel free to ask, and I will assist as much as I can.

@kvpt
Copy link
Contributor

kvpt commented Jun 12, 2019

@jsuvanto I made a build from my fix branch Xamarin.Forms.4.1.0.zip
You can test it to confirm that it fix your issue or if it is not related.

@jsuvanto
Copy link
Author

@kvpt I confirm this package fixes the problem. The EmptyView string is displayed as expected and there is no exception thrown when adding an item to the ObservableCollection.

@kvpt
Copy link
Contributor

kvpt commented Jun 13, 2019

@samhouts Do you want that I make a PR for this specific issue to be merged faster than the global PR #6467. And if yes, which branch to target ?

@samhouts
Copy link
Contributor

samhouts commented Jun 13, 2019

@kvpt Sounds good! Thanks! (also, 4.1.0 is good!)

@samhouts samhouts added e/2 🕑 2 and removed s/unverified New report that has yet to be verified labels Jun 13, 2019
@kvpt
Copy link
Contributor

kvpt commented Jun 13, 2019

@samhouts It's done.
Unfortunately I was not able to reproduce the issue myself, so I'm not able to provide a test for this.

@jsuvanto
Copy link
Author

@samhouts Do you still need a reproduction?

@samhouts
Copy link
Contributor

@jsuvanto No, I think we're good! If the stable version of 4.1.0 (to be released) doesn't resolve your issue, however, we may need you to reopen the issue. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/collectionview e/2 🕑 2 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/Android t/bug 🐛
Projects
None yet
Development

No branches or pull requests

5 participants