1

I'm trying to use lua scripts on my C# application, but I'm having problems to run different scripts, when I run 3~4 scripts I keep getting this exception:

enter image description here

As you can see I'm "catching" the AccessViolationException exception, just in case:

[HandleProcessCorruptedStateExceptions]
            public void ExecuteAsync(MyLuaThread thread)
            {
                Task.Factory.StartNew(() =>
                {
                    if (Monitor.TryEnter(ObjLocker, new TimeSpan(0, 0, 5)))
                    {
                        try
                        {
                            Thread.Sleep(10);
                            if (ScriptRunning[thread.name])
                            {
                                var result = lua.DoString(thread.code);
                            }
                            Thread.Sleep(10);
                        }
                        catch (AccessViolationException e4)
                        {
                            thread.errors.Add(time() + " - Access Error - " + e4.Message);
                            print("(" + thread.name + ") Access Error - " + e4.Message);
                        }
                        catch (NLua.Exceptions.LuaScriptException e)
                        {
                            thread.errors.Add(time() + " - Script Error - Line: " + e.Message.Substring(e.Message.IndexOf(":")));
                            print("(" + thread.name + ")Line: " + e.Message.Substring(e.Message.IndexOf(":")));
                        }
                        catch (Exception otherEx)
                        {
                            thread.errors.Add(time() + " - Other Error - " + otherEx.Message);
                            print("(" + thread.name + ") Access Error - " + otherEx.Message);
                        }
                        finally
                        {
                            Thread.Sleep(10);
                            Monitor.Exit(ObjLocker);
                        }
                    }
                });
            }

I created this small project to show you guys the error happening. Just add 3~5 threads and start them, it shouldn't take more than 5 seconds to the crash happen.

Small Project link: https://www.dropbox.com/s/4fyk8tblff7v5wa/SmallLuaTest.rar

Kyore
  • 388
  • 2
  • 7
  • 29
  • I don't see the problem, you have the debugger set up to break on an exception, so it does. If you want to see your catch work, just hit the continue button. – Ron Beyer Dec 10 '15 at 03:09
  • threads + lua doesnt sound so good. Is each thread having its own lua state or are all threads editing the same lua state locklessly? – Rochet2 Dec 10 '15 at 17:44
  • @RonBeyer That's the point, it doesnt hit the catch block, and if Im not debbuging it, just running the program it crashs. – Kyore Dec 11 '15 at 23:32
  • @Rochet2 I talked to the NLua creator about that, he said NLua is thread-safe and it should work, he will take a look in this small project soon, anyway, if I create different Lua states it doesn't crash, but I lose the point of my software, where scripts can share variables and functions between them. – Kyore Dec 11 '15 at 23:34

0 Answers0