Attempting To Read or Write Protected Memory

Topics: User Forum
Sep 23, 2006 at 7:50 PM
I've tried to run the 0.1.3 Beta release and some of the recent code check-ins, but all seems to give me a AccessViolationException in JS_EvaluateScript.

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at SpiderMonkey.JS.JS_EvaluateScript(IntPtr cx, IntPtr obj, String bytes, UInt32 length, String filename, UInt32 lineno, IntPtr rval)
at smnettest.Program.Main(String[] args) in C:\tmp\smdn6272\spidermonkeydotnet\development\smnet-test\Program.cs:line 93

I'm using Visual C# 2005 Express Edition to compile and run the code.

- petter
Coordinator
Sep 24, 2006 at 1:33 AM
Ok this should be fixed in 0.1.4.
Sep 25, 2006 at 2:38 PM
Ok, so I tested the 0.1.4 version but (if stressed) the problem still remains.

It seems to be that some callbacks are performed on delegates that has been collected by the GC.

The script I ran was:
for (var i = 0; i < 100000; i++)
{
var c = new Customer();c.name = 'Franky';c.age =32;c.computeReduction();c.age
}

And the response was:
CallbackOnCollectedDelegate was detected
Message: A callback was made on a garbage collected delegate of type 'spidermonkey-dotnet!SpiderMonkey.JS+JS_ResolveDelegate::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at SpiderMonkey.JS.JS_EvaluateScript(IntPtr cx, IntPtr obj, String bytes, UInt32 length, String filename, UInt32 lineno, IntPtr rval)
at smnettest.Program.Main(String[] args) in C:\tmp\smdn-0.1.4\smnet-test\Program.cs:line 97

- petter
Coordinator
Sep 25, 2006 at 3:21 PM
This is a bug not in the binding (as it is), but in the test code. Handling the "CallbackOnCollectedDelegate" errors are a fact of life in interop code, but simply solved. I've already been working at different approaches, so I'll update the test project soon with a fix. In simple situations such as the Customer example, adding the delegates as static instance variables is a good solution.
Coordinator
Sep 25, 2006 at 3:43 PM
This discussion has been copied to Work Item 3691. You may wish to continue further discussion there.
Coordinator
Sep 25, 2006 at 11:54 PM
fixed changeset 6765
Coordinator
Sep 26, 2006 at 6:02 PM
This discussion has been copied to Work Item 3732. You may wish to continue further discussion there.