AccessViolationException when using back button?

On Windows Phone 8, What is the correct usage of handling the backbutton and then calling a method in Unity?

I have the following code…

private void PhoneApplicationPage_BackKeyPress(object sender, CancelEventArgs e)
  {
            //e.Cancel = UnityApp.BackButtonPressed();
            e.Cancel = TouchManager.GetInstance().BackButton(); // Unity method to handle back button
  }

Initially I was using the first line of code that is commented out… but that didn’t allow me to show a popup, to confirm the user wanted to quit, the app would just quit.

Using the second method works, but in several places I get an AccessViolationException, such as when the game is loading, I press the Windows key, then the back button again, or if I cancel in IAP purchase with the back button or open the share charm.

Has anyone implemented the back button correctly and how can I safely call Unity methods from it?

Thanks

It seems we’re having this problem in several places on WP8, not just using the back button.
We have rolled our own IAP system in a XAML/C# solution… and we also get the ViolationAccessException in this code…

 private static void OnProductPurchase(string productidentifier)
        {
            Deployment.Current.Dispatcher.BeginInvoke(async () =>
            {
                try
                {
                    var licenseInformation = CurrentApp.LicenseInformation;
 
                    if (licenseInformation.IsTrial)
                        await PurchaseProductAsync();
 
                    if (licenseInformation.IsTrial)
                    {
                        WindowsStoreManager.ProductPurchaseFailed(TextsFile.GetText(StringConst.STR_KEY.IAP_PURCHASE_FAILED));
                        return;
                    }
 
                    var listingInformation = await CurrentApp.LoadListingInformationAsync();
                    
                    var productId = productidentifier;
 
                    try
                    {
                        await CurrentApp.RequestProductPurchaseAsync(productId, false);
                        var productLicenses = CurrentApp.LicenseInformation.ProductLicenses;
                        var license = productLicenses[productId];
                        if (license.IsActive)
                            WindowsStoreManager.ProductPurchased(productidentifier);
                        else
                            WindowsStoreManager.ProductPurchaseFailed("Test");
                    }
                    catch (Exception ex)
                    {
                        WindowsStoreManager.ProductPurchaseFailed("FAILED");
                    }
 
                }
                catch (Exception)
                {
                    WindowsStoreManager.ProductPurchaseFailed(TextsFile.GetText(StringConst.STR_KEY.IAP_PURCHASE_FAILED));
                }
 
            });
        }

Is there any behaviour in the above code that could be causing this?
Whenever we try to call back to WindowsStoreManager, we get the ViolationAccessException.

Thanks

I’m not really sure why would you comment out that line:

e.Cancel = UnityApp.BackButtonPressed();

You should handle it in scripts checking if escape button was pressed (that’s what back button translates to in Unity).

I’m not really sure what is the WindowsStoreManager class (instance?) you’re using. Google didn’t return any results either.

I’ll need to take another look at using escape, because on the Windows Phone I tested with, it just exited (or deactivated) the game if nothing was trapping it. And the only way to trap it (i.e to show a confirm quit popup), seemed to be using this technique.

Regarding the second problem, the ViolationAccessException seems to be caused by Resources.Load being called too early before the phone has had a chance to resume back into Unity.

The only way to prevent it from crashing when the game is in beta store test mode, was to put a coroutine with a yield of 2 seconds on the the method that shows the ‘Purchase failed’ popup in my game. So you guys might want to check that out.

I can submit a bug report too.

Please do report the bug. We’ll look into it.

As for trapping the escape key:

e.Cancel = UnityApp.BackButtonPressed();

That’s the line that traps it.

Thanks for the tip, Meltdown. I had the same issue and fixed using the WaitforSeconds trick. In my case, accessviolation happens after a call to a store function (requestProduducList, requestpurchase,etc) and I try to make a GUI update according to the returned event.

These issues need to be resolved by calling back onto the correct thread.

I would strongly suggest reading the Unity → WP8 porting guide we put together here…
Game development for desktop | Unity, for the thread specific stuff look at pages 12 and 15 on the 2nd Windows Phone doc.

Any suggestions/feedback on the docs are welcome.