NullReferenceException creating a WP8.1 Plugin

Hi,

I’m trying to set up a plugin for WP8.1 but I’m having some issues.
What I’m trying to do is creating an event from Visual Studio side this way:
public class MyPlugin {
public delegate void myDelegate(string message);
public static event myDelegate OnMyEvent;
[…]

myFunction() {
OnMyEvent(someString);
}
}

And then, use it from Unity side:
void OnEnable() {
PluginNamespace.MyPlugin.OnMyEvent += MyUnityFunction;
}
void OnDisable() {
PluginNamespace.MyPlugin.OnMyEvent -= MyUnityFunction;
}

This have been working fine until I had to use a coroutine inside MyUnityFunction:
private MyUnityFunction(string someString) {
StartCoroutine(MyCoroutineWWW);
}

When I call the coroutine from an event launched from Visual Studio code, it throws the following exception:
{System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at UnityEngineProxy.InternalCalls.MonoBehaviour_CUSTOM_StartCoroutine_Auto(Object self, Object routine)
at UnityEngine.MonoBehaviour.StartCoroutine_Auto(IEnumerator routine)
at UnityEngine.MonoBehaviour.StartCoroutine(IEnumerator routine)}

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Can anyone help me? I’m stuck at this point.

Did you try enabling exceptions via Debug->Exceptions?

It seems to be crashing in MyCoroutineWWW

Yes, as I said in the first post it is failing when I call the coroutine, but the coroutine fails no matter what I put inside, currently the coroutine only has one line of code (for testing purposes): yield return 0;
The exceptions are enabled, I also attached a screenshot of the exception thrown in the first post.

It’s like I’m having some kind of thread problem, isn’t it?

Thanks for any help.

Which Unity version are you on?

I’m using Version 4.6.1f1

You should check for null before invoking it:

void myFunction()
{
    if (OnMyEvent != null)
        OnMyEvent(someString);
}

Yes, I’m doing that in the real code, still crashing.

So coroutine is invoked from MyUnityFunction, what calls that function?

MyUnityFunction is called when the event triggers in the WP8 Plugin (developed in Visual Studio).

void OnEnable() {
PluginNamespace.MyPlugin.OnMyEvent += MyUnityFunction;
}

Might this be a threading issue? Do you make sure that the callback to StartCoroutine happens on main Unity thread?

It is my worry, I also think it is a thread issue, but I don’t know how to make sure that the callback happens on the main thread.

Any suggestion?
Thanks.

Try using these Unity - Scripting API: WSA.Application.InvokeOnAppThread, You can also access InvokeOnAppThread from App.xaml.cs if needed

So, where should I use InvokeOnAppThread, in Visual Studio code or in Unity?
Can you post some example code please? I’m a bit confused here.

Thanks.

In your case, it would probably be:

private MyUnityFunction(string someString) {
            UnityEngine.WSA.Application.InvokeOnAppThread(() =>
            {
               StartCoroutine(MyCoroutineWWW);
            }, false);
}

or you could do it in place where MyUnityFunction is called, let’s pretend that it’s being called in App.xaml.cs

AppCallbacks.Instance.InvokeOnAppThread(()=>
{
     MyUnityFunction("sdsdsd");
}, false);
1 Like

It works!

Used the UnityEngine.WSA.Application.InternalInvokeOnAppThread solution (in my case, it is InvokeOnAppThread, not InternalInvokeOnAppThread, but its ok).

Thank you very much Tomas1856.