I keep getting AccessViolationException, any ideas?

Hey Guys,

As part of our school project in Arduino, we have the mobile application part, and we used corona to build it cause I’m familiar with the platform, we must use windows phone 8 (project limitations :\  ), so we ended up using Corona Cards.

When running the application, it runs for a minute and then we get this exception:

System.AccessViolationException

and it says it gets it from the line (which is present in one of our modules called Utils.cs):

MainPage.coronaRuntime.CoronaRuntimeEnvironment.DispatchEvent(eventArgs);

This is the code we have in the MainPage:

   public partial class MainPage : PhoneApplicationPage    {         public MainPage()         {             // Initialize this page's components that were set up via the UI designer.             InitializeComponent();             // Set up Corona to automatically start up when the control's Loaded event has been raised.             // Note: By default, Corona will run the "main.lua" file in the "Assets\Corona" directory.             //       You can change the defaults via the CoronaPanel's AutoLaunchSettings property.             fCoronaPanel.AutoLaunchEnabled = true;             // Set up the CoronaPanel control to render fullscreen via the DrawingSurfaceBackgroundGrid control.             // This significantly improves the framerate and is the only means of achieving 60 FPS.             fCoronaPanel.BackgroundRenderingEnabled = true;             fDrawingSurfaceBackgroundGrid.SetBackgroundContentProvider(fCoronaPanel.BackgroundContentProvider);             fDrawingSurfaceBackgroundGrid.SetBackgroundManipulationHandler(fCoronaPanel.BackgroundManipulationHandler);             // Add a Corona event handler which detects when the Corona project has been started.             fCoronaPanel.Runtime.Started += OnCoronaRuntimeStarted;             fCoronaPanel.Runtime.Loaded += OnCoronaRuntimeLoaded;             PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;         }         public static CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs coronaRuntime = null;         private void OnCoronaRuntimeStarted(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)         {             coronaRuntime = e;         }         private void OnCoronaRuntimeLoaded(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)         {             e.CoronaRuntimeEnvironment.AddEventListener("GetMessageFromLua", Utils.OnGetMessageFromLua);         }     }

Please notice this field, 

public static CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs coronaRuntime = null;

we store it inside MainPage and initialise it in “OnCoronaRuntimeStarted” (also tried from “OnCoronaRuntimeLoaded” and got the same results)

Its STATIC because we want to use it from other modules to pass messages to the Lua side.

And when calling it (like mentioned above) we get that exception but only after a while.

Any help will be greatly appreciated.

Roy.

I would suggest that you go over your C# code and check for null references.

This is because an AccessViolationException can be thrown instead of a NullReferenceException (due to null reference usage) if the .NET code was called by the C++/CX side of the application.  I think it’s because Microsoft COM (which C++/CX uses to interface with .NET) catches the .NET exceptions and turns them into less helpful COM errors as I’m sure you’re seeing here.  If the stack trace doesn’t provide any useful info and adding null references checks in the code don’t fix the issue, then you’ll have to debug it old-school style and start commenting-out code until you’ve isolated the issue.

Joshua, 

Thank you for your reply.

I did everything i could to try and find the issue, ill appreciate your help here.

Here is the line that causes the exception:

File%2023-07-2016%2020%2002%2017_zpsttwf

The thing is that the “pack” seems OK, when exploring it with the tooltip

File%2023-07-2016%2020%2001%2000_zpsknod

It has 2 properties (count is 2) witch seems ok since the 1st one should be the name of the pack (to be captured by the lua) and the second is the message i want to add to it.

on the lua side i have this in my main.lua to capture the messages:

local function onGetMessageFromCSharp(event) if (event == nil or event.message == nil or event.message == "") then return; end     print("GetMessageFromCSharp :"..event.message);     EventHandler.ParseEvent(tostring(event.message)); end Runtime:addEventListener( "GetMessageFromCSharp", onGetMessageFromCSharp);

It does sometimes capture the first few messages from c# but then it crashes with the exception mentioned above.

Any suggestions? I’m lost here :\

Any more details you need from me? just let me know

Thanks in advance.

Roy.

The exception states that this is happening in “Cipher.dll”  I assume this must be your library, right?

As in, your application is named Cipher or your have a separate .NET library named Cipher.

You can try to isolate it by putting breakpoints in the .NET methods that Lua calls, because this is likely where the exception is happening.

You see, the DispatchEvent() method you are calling in .NET is calling your Lua onGetMessageFromCSharp() function, which in turn is likely calling something on the .NET side in your Cipher library… and the exception is stating that this error is happening somewhere in the Cipher library.

I would suggest that you go over your C# code and check for null references.

This is because an AccessViolationException can be thrown instead of a NullReferenceException (due to null reference usage) if the .NET code was called by the C++/CX side of the application.  I think it’s because Microsoft COM (which C++/CX uses to interface with .NET) catches the .NET exceptions and turns them into less helpful COM errors as I’m sure you’re seeing here.  If the stack trace doesn’t provide any useful info and adding null references checks in the code don’t fix the issue, then you’ll have to debug it old-school style and start commenting-out code until you’ve isolated the issue.

Joshua, 

Thank you for your reply.

I did everything i could to try and find the issue, ill appreciate your help here.

Here is the line that causes the exception:

File%2023-07-2016%2020%2002%2017_zpsttwf

The thing is that the “pack” seems OK, when exploring it with the tooltip

File%2023-07-2016%2020%2001%2000_zpsknod

It has 2 properties (count is 2) witch seems ok since the 1st one should be the name of the pack (to be captured by the lua) and the second is the message i want to add to it.

on the lua side i have this in my main.lua to capture the messages:

local function onGetMessageFromCSharp(event) if (event == nil or event.message == nil or event.message == "") then return; end     print("GetMessageFromCSharp :"..event.message);     EventHandler.ParseEvent(tostring(event.message)); end Runtime:addEventListener( "GetMessageFromCSharp", onGetMessageFromCSharp);

It does sometimes capture the first few messages from c# but then it crashes with the exception mentioned above.

Any suggestions? I’m lost here :\

Any more details you need from me? just let me know

Thanks in advance.

Roy.

The exception states that this is happening in “Cipher.dll”  I assume this must be your library, right?

As in, your application is named Cipher or your have a separate .NET library named Cipher.

You can try to isolate it by putting breakpoints in the .NET methods that Lua calls, because this is likely where the exception is happening.

You see, the DispatchEvent() method you are calling in .NET is calling your Lua onGetMessageFromCSharp() function, which in turn is likely calling something on the .NET side in your Cipher library… and the exception is stating that this error is happening somewhere in the Cipher library.