Xamarin.forms: [MacOS] Native NRE exception when removing children from a StackLayout within a StackLayout

Created on 20 Dec 2018  路  8Comments  路  Source: xamarin/Xamarin.Forms

Description

In the sample app, click "Remove", and watch the app explode.

A NullReferenceException occurs when removing children from a StackLayout within a StackLayout, but only within certain circumstances (for example, if you comment out the contents of the MainPage constructor, the crash no longer seems to happen). Additionally, the bug didn't seem to happen in the iOS host application (in the simulator); it seems to be specific to the MacOS renderer. Here's the entire application log:

2018-12-20 12:30:03.057 NREBug.MacOS[17773:4970177] View <Xamarin_Forms_Platform_MacOS_LabelRenderer: 0x600003867340> has 1 views pointing at it that can't be found.
Stacktrace:

  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) AppKit.NSApplication.NSApplicationMain (int,string[]) [0x0005a] in <5509c2c8ada64120b6edf308eda0995f>:0
  at AppKit.NSApplication.Main (string[]) [0x00040] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/AppKit/NSApplication.cs:100
  at NREBug.MacOS.MainClass.Main (string[]) [0x00007] in /Users/jwostenberg/Code/NREBug/NREBug.MacOS/Main.cs:7
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) [0x00051] in <84a219c2a1bb4d9cac4cf12fe0d154d4>:0

Native stacktrace:

    0   NREBug.MacOS                        0x0000000102b169d8 mono_handle_native_crash + 264
    1   NREBug.MacOS                        0x0000000102a93856 altstack_handle_and_restore + 70
    2   libobjc.A.dylib                     0x00007fff5d41a59d objc_msgSend + 29
    3   AppKit                              0x00007fff2e85ebb0 -[NSView _removeFromKeyViewLoop] + 203
    4   AppKit                              0x00007fff2e85e561 -[NSView _finalize] + 892
    5   AppKit                              0x00007fff2e85e05e -[NSView dealloc] + 209
    6   AppKit                              0x00007fff2e8f3203 -[NSControl dealloc] + 148
    7   AppKit                              0x00007fff2e90fd6e -[NSTextField dealloc] + 124
    8   libobjc.A.dylib                     0x00007fff5d41d7c2 _ZN12_GLOBAL__N_119AutoreleasePoolPage3popEPv + 726
    9   CoreFoundation                      0x00007fff311ec012 _CFAutoreleasePoolPop + 22
    10  Foundation                          0x00007fff335b6a5e -[NSAutoreleasePool drain] + 144
    11  AppKit                              0x00007fff2e7561d4 -[NSApplication run] + 810
    12  AppKit                              0x00007fff2e7458a3 NSApplicationMain + 780
    13  ???                                 0x00000001090cb1ce 0x0 + 4446794190
    14  ???                                 0x00000001090caefb 0x0 + 4446793467
    15  NREBug.MacOS                        0x0000000102b2acd1 mono_jit_runtime_invoke + 1441
    16  NREBug.MacOS                        0x0000000102c2668f mono_runtime_invoke_checked + 127
    17  NREBug.MacOS                        0x0000000102c2d50e mono_runtime_exec_main_checked + 110
    18  NREBug.MacOS                        0x0000000102a89889 mono_jit_exec + 297
    19  NREBug.MacOS                        0x0000000102a8c884 mono_main + 11156
    20  NREBug.MacOS                        0x0000000102a29b19 xamarin_main + 1305
    21  NREBug.MacOS                        0x0000000102a2aae4 main + 36
    22  libdyld.dylib                       0x00007fff5e4efed9 start + 1

Debug info from gdb:

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

=================================================================
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.
=================================================================

Thread finished: <Thread Pool> #5
2018-12-20 12:30:09.362 NREBug.MacOS[17773:4970177] 
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at (wrapper managed-to-native) AppKit.NSApplication.NSApplicationMain(int,string[])
  at AppKit.NSApplication.Main (System.String[] args) [0x00040] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/AppKit/NSApplication.cs:100 
  at NREBug.MacOS.MainClass.Main (System.String[] args) [0x00007] in /Users/jwostenberg/Code/NREBug/NREBug.MacOS/Main.cs:7 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at (wrapper managed-to-native) AppKit.NSApplication.NSApplicationMain(int,string[])
  at AppKit.NSApplication.Main (System.String[] args) [0x00040] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/AppKit/NSApplication.cs:100 
  at NREBug.MacOS.MainClass.Main (System.String[] args) [0x00007] in /Users/jwostenberg/Code/NREBug/NREBug.MacOS/Main.cs:7 

Steps to Reproduce

  1. Create an app with a ContentPage containing StackLayout containing a StackLayout
  2. Initialize the innermost StackLayout with some children (does not have to be a Label, as in the example)
  3. Add a button that removes a child from the StackLayout -- this button will trigger the crash.

Here is MainPage.xaml:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:NREBug" x:Class="NREBug.MainPage">
    <StackLayout>
        <Button Text="Add" TextColor="Black" Clicked="OnAddClicked" />
        <Button Text="Remove" TextColor="Black" Clicked="OnRemoveClicked"/>
        <StackLayout x:Name="TheItems"></StackLayout>
    </StackLayout>
</ContentPage>

And here is MainPage.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace NREBug {
    public partial class MainPage : ContentPage {
        public MainPage() {
            InitializeComponent();
            AddItem();
        }

        void AddItem() {
            TheItems.Children.Add(new Label { Text = "Placeholder" });
        }

        void OnAddClicked(object sender, EventArgs args) {
            AddItem();
        }

        void OnRemoveClicked(object sender, EventArgs args) {
            if (TheItems.Children.Count > 0) {
                TheItems.Children.RemoveAt(0);
            }
        }
    }
}

Expected Behavior

The child view is removed from the StackLayout.

Actual Behavior

A NullReferenceException is thrown from native code.

Basic Information

  • Version with issue: Xamarin.Forms 3.4.0.1008975
  • Last known good version: None
  • IDE: Visual Studio Community for Mac 7.7.2 + XCode 10.1 (10B61)
  • Platform Target Frameworks:

    • MacOS: 10.14

  • Nuget Packages: None
  • Affected Devices: MacOS

Reproduction Link

Reproduction: NREBug.zip

in-progress macOS bug

All 8 comments

Can any one find the solution of this
facing same issue only for MAC platform after updating the Xamarin Lib, XCode, VS for MAC

I create my Custom control window and use that custom control into my main.xaml file while closing that custom control window null reference exception fired for perficular MAC Platform only for other plafrom IOS, Android, and UWP it is working fine.

StackTrace

at (wrapper managed-to-native) AppKit.NSApplication.NSApplicationMain(int,string[]) at AppKit.NSApplication.Main (System.String[] args) [0x00040] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/AppKit/NSApplication.cs:100 at MyApplication.MainClass.Main (System.String[] args) [0x00017] in /MyApplication/Main.cs:11

HResult -2147467261
Message "Object reference not set to an instance of an object"

Static members => -Non-public members => _COMPlusExceptionCode -532462766

Non-public members => IsTransient System.NotImplementedException: The method or operation is not implemented.

_Hresult -2147467261
_className "System.NullReferenceException" _message "Object reference not set to an instance of an object"
_stackTrace {system.IntPtr[9]}
[0] 0x1106de21d
[1] 0x0
[2] 0x7fbe00350378
[3] 0x1106ddf4a
[4] 0x0
[5] 0x7fbe00350308
[6] 0x10978dc3a
[7] 0x0
[8] 0x7be0081e488

@vivek00712 Yep, seems like the same issue.

I've noticed it only occurs for me when removing elements that were added _during_ initialization. (though I haven't thoroughly isolated that fact, so it's possible there's another factor at play)

Also, I would attempt to bugfix this myself, but the fact that the stacktrace is totally useless (since it just points to Main, and goes no deeper) is total brick wall for me. I have no idea how to go about debugging this thing. Any thoughts on how to go about figuring out what's wrong internally?

@jwosty yes, I am working on same still can not able to fix the same. In my case Internally it is throwing the System.NotImplementedException.
Have you find the solution or other better convenient way to solve it out?

@vivek00712 Unfortunately I haven't. This is definitely something that I will have to deal with eventually though; it's a showstopper for my app to not be able to delete items. :/ Currently I'm not working around it.

I'm not actually seeing any sort of inner NotImplementedException in mine -- interesting, thanks for the info. Maybe that means our issues' root causes are actually different? I'll keep looking into that.

@vivek00712 out of curiosity, do you see anything similar to the 2018-12-20 12:30:03.057 NREBug.MacOS[17773:4970177] View <Xamarin_Forms_Platform_MacOS_LabelRenderer: 0x600003867340> has 1 views pointing at it that can't be found. line that I'm getting right before the crash?

Seems related to #4156 and #4300

This is the stack trace, i guess:

  at (wrapper managed-to-native) Foundation.NSObject.xamarin_release_managed_ref(intptr,Foundation.NSObject)
  at Foundation.NSObject.ReleaseManagedRef () [0x00001] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/Foundation/NSObject2.cs:235 
  at Foundation.NSObject.Dispose (System.Boolean disposing) [0x00030] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/Foundation/NSObject2.cs:788 
  at AppKit.NSView.Dispose (System.Boolean disposing) [0x00008] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/AppKit/NSView.g.cs:11348 
  at AppKit.NSControl.Dispose (System.Boolean disposing) [0x00001] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/AppKit/NSControl.g.cs:1493 
  at AppKit.NSTextField.Dispose (System.Boolean disposing) [0x00001] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/AppKit/NSTextField.g.cs:1302 
  at Foundation.NSObject.Dispose () [0x00001] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/Foundation/NSObject2.cs:147 
  at Xamarin.Forms.Platform.MacOS.ViewRenderer`2+<>c__DisplayClass32_0[TView,TNativeView].<Dispose>b__0 () [0x00000] in /Users/andrei/Desktop/forms/Xamarin.Forms.Platform.iOS/ViewRenderer.cs:127 
  at Foundation.NSAsyncActionDispatcher.Apply () [0x00002] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/Foundation/NSAction.cs:152 
  at (wrapper managed-to-native) AppKit.NSApplication.NSApplicationMain(int,string[])
  at AppKit.NSApplication.Main (System.String[] args) [0x00040] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.12/src/Xamarin.Mac/AppKit/NSApplication.cs:100 
  at Xamarin.Forms.ControlGallery.MacOS.MainClass.Main (System.String[] args) [0x00007] in /Users/andrei/Desktop/forms/Xamarin.Forms.ControlGallery.MacOS/Main.cs:10 

Cannot reproduce
I presume, https://github.com/xamarin/Xamarin.Forms/pull/4962 fixed this issue

Was this page helpful?
0 / 5 - 0 ratings