Xamarin Android - Run time crash - [libc] Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x7ffb9b4f40 in tid 32564 (companyname.app), pid 32564 (companyname.app)

gagrim808 26 Reputation points
2022-06-11T12:58:45.73+00:00

I am creating an app for Android in Xamarin Forms (latest) on Visual Studio. On my Android 12.1 emulator, it works fine, whether I use Target Framework as 12, 11, or 10.
But on my actual smartphone (API level 10), and on another smartphone (API level 7), it seems to crash as soon as I run this function -

        void Browsing(object sender)  
        {  
            Browser.Children.Clear();  
              
            FocusedItem = null;  
              
            CurrentFolder.Insert(0, ((sender as Frame).BindingContext as FolderFunctionality.Item).Name);  
  
            FolderFunctionality.Item Folder = FolderFunctionality.Get(CurrentFolder.GetRange(1, CurrentFolder.Count - 1), CurrentFolder[0]);  
  
            foreach (FolderFunctionality.Item a in Folder)  
            {  
                Frame button = new Frame  
                {  
                    Padding = 10,  
                    CornerRadius = 15,  
                    HasShadow = false,  
                    BackgroundColor = Color.LightSkyBlue,  
                    Content = new StackLayout  
                    {  
                        HorizontalOptions = LayoutOptions.FillAndExpand,  
                        Orientation = StackOrientation.Horizontal,  
                        Padding = 0,  
                        Spacing = 10,  
                        Children =  
                        {  
                            new Image { BackgroundColor = Color.Transparent, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Start, HeightRequest = 50, WidthRequest = 50, Source = ImageSource.FromResource("App.Resources.Devices." + a.Type + ".png", typeof(App)) },  
                            new Label { BackgroundColor = Color.Transparent, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.FillAndExpand, LineBreakMode = LineBreakMode.TailTruncation, TextColor = Color.Black, Padding = 0, FontSize = 20, Text = a.Name },  
                        }  
                    }  
                };  
                TapGestureRecognizer tap = new TapGestureRecognizer();  
  
                button.BindingContext = a;  
  
                if (a.Type == "F") tap.Tapped += (snd, arg) =>  
                {  
                    PreviouslyFocusedItem.BackgroundColor = Color.LightSkyBlue;  
                    ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.TailTruncation;  
  
                    PreviouslyFocusedItem = snd as Frame;  
  
                    bool UnFocus = !(snd as Frame).HasShadow;  
                    Delays.Cancel();  
                    Delays.Dispose();  
                    Delays = new CancellationTokenSource();  
  
                    PreviouslyFocusedItem.BackgroundColor = Color.DeepSkyBlue;  
                    ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.WordWrap;  
  
                    if (UnFocus)  
                    {  
                        FocusedItem = ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).Text;  
                        Task.Run(async () =>  
                        {  
                            try { await Task.Delay(500, Delays.Token); } catch (OperationCanceledException) { }  
                            PreviouslyFocusedItem.HasShadow = false;  
                        });  
                        (snd as Frame).HasShadow = true;  
                    }  
                    else Browsing(snd);  
                };  
                else tap.Tapped += (snd, arg) =>  
                {  
                    PreviouslyFocusedItem.BackgroundColor = Color.LightSkyBlue;  
                    ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.TailTruncation;  
  
                    PreviouslyFocusedItem = snd as Frame;  
  
                    bool UnFocus = !(snd as Frame).HasShadow;  
                    Delays.Cancel();  
                    Delays.Dispose();  
                    Delays = new CancellationTokenSource();  
  
                    PreviouslyFocusedItem.BackgroundColor = Color.DeepSkyBlue;  
                    ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.WordWrap;  
  
                    if (UnFocus)  
                    {  
                        FocusedItem = ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).Text;  
                        Task.Run(async () =>  
                        {  
                            try { await Task.Delay(500, Delays.Token); } catch (OperationCanceledException) { }  
                            PreviouslyFocusedItem.HasShadow = false;  
                        });  
                        (snd as Frame).HasShadow = true;  
                    }  
                    else DeviceController(snd);  
                };  
  
                button.GestureRecognizers.Add(tap);  
                Browser.Children.Add(button);  
            }  
  
            if (Browser.Children.Count < 1) Browser.Children.Add(new Label { Text = "No Items Here", BindingContext = new FolderFunctionality.Item() });  
  
            maincontent.Content = new ScrollView { Content = Browser, HorizontalScrollBarVisibility = ScrollBarVisibility.Never };  
            topbar.Content = new StackLayout  
            {  
                Padding = new Thickness(8, 4),  
                Spacing = 0,  
                BackgroundColor = Color.Transparent,  
                Orientation = StackOrientation.Horizontal,  
                Children =  
                {  
                    backbutton,  
                    AppName,  
                    new Frame { Padding = 0, WidthRequest = 62, HeightRequest = 62, BackgroundColor = Color.Transparent }  
                }  
            };  
            MainPage = new ContentPage                                                              // Main Page  
            {  
                Opacity = 0,  
                Content = new StackLayout  
                {  
                    Spacing = 0,  
                    Children =  
                    {  
                        topbar,  
                        maincontent,  
                        ChangeOptions  
                    }  
                }  
            };  
  
            MainPage.FadeTo(1);  
        }  

Where FolderFunctionality and FolderFunctionality.Item is a class created by me (Very less chances that it causes problems as another function using it in the same way runs as expected).

The console shows this message last -
[libc] Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x7ffb9b4f40 in tid 32564 (companyname.app), pid 32564 (companyname.app)

Any ideas about the solution will be highly appreciated.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,040 questions
{count} votes

1 answer

Sort by: Most helpful
  1. gagrim808 26 Reputation points
    2022-06-15T14:16:15.24+00:00

    I think the problem is -
    'Browsing()' is called as a part of a button click event which is a child of Browser'. And, Browser's children are being cleared in 'Browsing()' itself.
    So, I guess the button object is destroyed before 'Browsing' finishes, and the nulls it out.
    Here is how Browsing() is called...

    tap.Tapped += (snd, arg) =>  
                    {  
                        PreviouslyFocusedItem.BackgroundColor = Color.LightSkyBlue;  
                        ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.TailTruncation;  
      
                        PreviouslyFocusedItem = snd as Frame;  
      
                        bool UnFocus = !(snd as Frame).HasShadow;  
                        Delays.Cancel();  
                        Delays.Dispose();  
                        Delays = new CancellationTokenSource();  
      
                        PreviouslyFocusedItem.BackgroundColor = Color.DeepSkyBlue;  
                        ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.WordWrap;  
      
                        if (UnFocus)  
                        {  
                            FocusedItem = ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).Text;  
                            Task.Run(async () =>  
                            {  
                                try { await Task.Delay(500, Delays.Token); } catch (OperationCanceledException) { }  
                                PreviouslyFocusedItem.HasShadow = false;  
                            });  
                            (snd as Frame).HasShadow = true;  
                        }  
                        else Browsing(snd);  
                    };  
    

    Am I correct, and if so, can you tell me the possible solution?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.