Forum Xamarin.iOS

Getting nullrefexception when marshaling call

RaymondKellyRaymondKelly USMember ✭✭✭

I am trying to make a call to get the iOS's device DNS server address. This actually works! However, when the class exits, the app crashes with a null ref exception. I don't have a clue why this is happening. Here are the relevant pieces of code:

DLL call:

            [DllImport("libresolv", EntryPoint = "res_9_ninit")]
            private static extern int res_ninit(ref res_state state);

The call:

res_state state = new res_state();
int result = res_ninit(ref state);

The struct:

            [StructLayout(LayoutKind.Sequential)]
            public struct res_state
            {
                public int retrans;
                public int retry;
                public ulong options;
                public int nameserversCount;
                public sockaddr_in socket_in;
            }

So when the call is made, the stuct is populated correctly with all information. But when the class leaves the function, I get the null ref exception. Here is the stack trace:

  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00012] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:667 
  at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x000a8] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:179 
  at System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00009] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:153 
  at System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00027] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/rttype.cs:5639 
  at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00039] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/activator.cs:201 
  at System.Activator.CreateInstance (System.Type type) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/activator.cs:134 
  at MineChat.MainPage.SelectItem (MineChat.MasterPageItem item) [0x0001c] in C:\Users\kellyray\Documents\Visual Studio 2017\Projects\MineChat\MineChat\MineChat\MainPage.xaml.cs:128 

I am sure I am doing something wrong with marshaling but I cant figure it out. Any help would be greatly appreciated!

Best Answer

  • RaymondKellyRaymondKelly US ✭✭✭
    Accepted Answer

    I figured this out. The res_state object was getting garbage collected. I moved that up to the class level and it fixed the issue.

Answers

  • RaymondKellyRaymondKelly USMember ✭✭✭
    Accepted Answer

    I figured this out. The res_state object was getting garbage collected. I moved that up to the class level and it fixed the issue.

Sign In or Register to comment.