FFImageLoading icon indicating copy to clipboard operation
FFImageLoading copied to clipboard

crash on Android using Xamarin.Forms 2.4.0 and FFImageLoading 2.2.22

Open grasdy opened this issue 9 years ago • 12 comments

Hello,

I get crash on Android using Xamarin.Forms 2.4.0 and FFImageLoading 2.2.22:

My xaml-page:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
             xmlns:local="clr-namespace:Bug"
             x:Class="Bug.ImgList">
    <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">        
                               <ListView
                                               HasUnevenRows="True"
                                               SeparatorVisibility="None"
                                               SeparatorColor="Transparent"
                                               IsGroupingEnabled="True"
                                               ItemsSource="{Binding ImageSrc}"
                                               ItemSelected="OnItemSelected"
                                               CachingStrategy="RecycleElement"
                                               VerticalOptions="FillAndExpand">
                                               <ListView.ItemTemplate>
                                                               <DataTemplate>
                                                                              <local:ViewCell/>
                                                               </DataTemplate>
                                               </ListView.ItemTemplate>
                               </ListView>
    </AbsoluteLayout>
</ContentPage>

Where ViewCell:

<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
          xmlns:ffimagetransforms="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations"
          x:Class="Bug.ViewCell">
    <Grid x:Name="ViewCell" Margin="0, 0, 0, 22" RowSpacing="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="72" />
            <ColumnDefinition Width="*"/> 
        </Grid.ColumnDefinitions>

        <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand" Grid.Column="0">
            <ffimageloading:CachedImage 
                Aspect="AspectFit"
                HeightRequest="40"
                WidthRequest="40"
                AbsoluteLayout.LayoutBounds="0.5, 0.5, AutoSize, AutoSize"
                AbsoluteLayout.LayoutFlags="PositionProportional"
                Source="{Binding Img}">
                <ffimageloading:CachedImage.Transformations>
                    <ffimagetransforms:CircleTransformation/>
                </ffimageloading:CachedImage.Transformations>
            </ffimageloading:CachedImage>
            <Label
                Text="{Binding Text}"
                FontSize="16"
                TextColor="White"
                AbsoluteLayout.LayoutBounds="0.5, 0.5, AutoSize, AutoSize" 
                AbsoluteLayout.LayoutFlags="PositionProportional"
                IsVisible="{Binding HasText}"/>
        </AbsoluteLayout>
    </Grid>
</ViewCell>

ImageSrc class:

public class ImageSrc
{
        public bool HasText { get; }

        public string Text
        {
                get
                {
                      return "TT";
                }
       }

       public ImageSource Img { get; } //here some different source values - some from Android resources, some from stream.
}

Create at least several hundreds of items and start scrolling list view up and down – you’ll get crash (on Android 7.0) with one of the following errors:

“JNI ERROR (app bug): attempt to use stale global reference” Or “JNI ERROR (app bug): accessed deleted global reference”

Seems to be race condition somewhere in image loading.

I don't have any crashes when I use FFImageLoading version 2.2.6 and 2.2.11.

grasdy avatar Oct 30 '17 16:10 grasdy

Hi @grasdy,

I used your code and cannot reproduce it. Could you make a working project out of the code you provided?

BTW: also using bindings in fast scrolling lists is a bad idea, read more here: https://github.com/luberda-molinet/FFImageLoading/wiki/Xamarin.Forms-Advanced#usage-in-listview-with-listviewcachingstrategyrecycleelement-enabled

daniel-luberda avatar Oct 30 '17 17:10 daniel-luberda

Hi @daniel-luberda,

I created the project in Visual Studio 2017 that shows this crash. Just build this project and deploy it on device with Android 7.0 or 6.0 (I also got this crash on Android 6.0). You should scroll ListView very fast for get this crash.

I also have saved Output-Debug.txt - output log file, with this crash (in archive). Crash.zip

grasdy avatar Oct 31 '17 11:10 grasdy

Hi,

I can confirm this "bug". Our customers started complaining about phone crashing in quite simple list view. I first though it was xamarin problem with list elements, but after removing FFImageLoading xaml element error stopped happening. I was able to reproduce error same way as grasdy explained here.

Rokk6 avatar Nov 03 '17 12:11 Rokk6

I debugged it. IMO it's still a Xamarin.Forms issue, that error cannot be reproduced when using newest 2.2.23 prerelease which uses fast renderers by default if possible (correct Xamarin.Forms version), when I disabled fast renderer, the error happened again. Probably too many views cause too many layout recalculations and too much load on a main thread.

daniel-luberda avatar Nov 03 '17 17:11 daniel-luberda

Please let me know if you still experience it in 2.2.23 stable

daniel-luberda avatar Nov 04 '17 12:11 daniel-luberda

I updated to 2.2.23 stable and it still seems to crash when fast scrolling, but I believe you are right about xamarin causing this error. I fixed this problem by removing caching strategy.

Rokk6 avatar Nov 06 '17 08:11 Rokk6

I updated to 2.2.23 and got this crash too. Removing CachingStrategy from ListView has fixed this problem.

I also tried to set CacheType="{x:Null}" for ffimageloading:CachedImage in ViewCell element (without removing CachingStrategy) and this crash became to show more difficult. I upload my project with this edits. Crash2.zip

It seems like you're trying to draw an image into a view that has been collected by Java garbage collector. Have you checked this case?

grasdy avatar Nov 07 '17 09:11 grasdy

It seems like you're trying to draw an image into a view that has been collected by Java garbage collector. Have you checked this case?

I don't think it's the case, this is checked before setting an image. See this:

  • https://github.com/luberda-molinet/FFImageLoading/blob/master/source/FFImageLoading.Droid/Targets/ImageViewTarget.cs#L46-L57
  • https://github.com/luberda-molinet/FFImageLoading/blob/master/source/FFImageLoading.Droid/Targets/ImageViewTarget.cs#L87

daniel-luberda avatar Nov 07 '17 20:11 daniel-luberda

I do not know much about xamarin.forms but just spent a whole day debugging an Xamarin vanilla android project that crashed without any usefull error. I had the same errors “JNI ERROR (app bug): attempt to use stale global reference” Or “JNI ERROR (app bug): accessed deleted global reference”

In my case the problem came from not cancelling the old loader task when loading a new image for recycled views in my gridview.

Maybe the task cancelling when recycling views is not functioning correctly in the forms implementation?

Sevenish avatar Nov 08 '17 00:11 Sevenish

@Sevenish Previous task is cancelled before every image loading: https://github.com/luberda-molinet/FFImageLoading/blob/master/source/FFImageLoading.Forms.Droid/CachedImageFastRenderer.cs#L236

Maybe we should add some locks there?

daniel-luberda avatar Nov 13 '17 15:11 daniel-luberda

Could you check if it's solved in 2.2.25?

daniel-luberda avatar Nov 13 '17 20:11 daniel-luberda

@daniel-luberda I check version 2.2.25 and it's look like this bug is fixed. Thanks a lot!

grasdy avatar Nov 14 '17 15:11 grasdy