This project has moved. For the latest updates, please go here.

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

Jul 8, 2016 at 10:53 AM
Hi, I am creating v8 script engine in thread and executing script. a list contains script files which are executed in thread one by one. when more number script files executed "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." exception is thrown and application gets crashed.
public async Task PlayProject(string projectData) 
{
       var engineInstance= new V8ScriptEngine();

       jsScript = File.ReadAllText(projectData);   
   
      Task.Factory.StartNew(() => {
                 var compiledScript = engineInstance.Compile(jsScript);
                engineInstance.Execute(compiledScript);     
       });    
}
Thanks
Praveen
Coordinator
Jul 8, 2016 at 2:22 PM
Hi Praveen,

Be sure to dispose each instance of V8ScriptEngine when you're done with it. That's absolutely essential in an application that uses multiple instances, as each one holds significant unmanaged resources that .NET's garbage collector can't track.

In addition, be careful with the number of active (undisposed) instances you have at a time. Each V8 runtime reserves a large chunk of address space, and a 32-bit process can easily reach the point of exhaustion with just a few dozen instances. For concurrent execution of multiple scripts, consider using a pool of engines, with a fixed size that's roughly equal to your CPU core count.

Good luck!
Jul 11, 2016 at 10:31 AM
Hi,

Thank you for reply. I implemented engine pool as as you suggested using ClearScript.Manager and exception is still happens and application gets crashed. this happens when script file list grows to 100-300 scripts. it executes success fully when we have few scripts files to execute. My script contains winform object,threads task and many other 3rd party component objects. Is it better to use multi process architecture for this kind of problems. so that only child process appdomain gets killed.
public async Task PlayProject(string projectData) 
{
       var engineInstance= new V8ScriptEngine();

       jsScript = File.ReadAllText(projectData);   
   
      Task.Factory.StartNew(() => {

                //__Start new child process from here__

                 var compiledScript = engineInstance.Compile(jsScript);
                engineInstance.Execute(compiledScript);     
       });    
}
Thanks
Praveen
Coordinator
Jul 11, 2016 at 3:45 PM
Hi Praveen,

It's difficult to diagnose your issue without more information, but if the crash happens during V8ScriptEngine construction, then it's nearly certain that you're running out of address space by creating too many active instances.

One of our pre-release tests is to run the SunSpider suite thousands of times within a single process, each in a separate V8ScriptEngine instance. As long as you dispose each instance when you're done with it, and you don't try to use a large number of instances concurrently, you should be OK.

If that's not how your application is crashing, we'll be happy to look at a stack trace or try to reproduce the crash if you can share some more of your code or a minimal crashing sample.

Cheers!
Jul 15, 2016 at 6:11 PM
Hi,

Please find my application sample to recreate above issue. I also included memory leak script.

Thanks
Praveen
Coordinator
Jul 15, 2016 at 8:08 PM
Edited Jul 15, 2016 at 8:11 PM
Hi again,

Thanks for sharing your sample!

In our tests, your application crashes even when the iteration count is set to 1. The script attempts to create an array of 1,000 strings, each of length 1,000,000 characters. With default settings, V8 simply can't do this in a 32-bit process; it runs out of memory and kills the process. It might be possible to use resource constraints to expand V8's limits, but no amount of tweaking is likely to make this particular script work.

A 64-bit process is able to complete this script, ending up with a working set of around 1.3 GB.

Cheers!