Skip to content

Possible memory leak in ScriptEngine.AddCOMType (V8ScriptEngine) #510

@rflsouza

Description

@rflsouza

Version: Microsoft.ClearScript.V8 7.3.7
Framework: .Net Framework 4.8

I haven't found a way to unload a COM. If it does not exist manually, I believe there is a leak problem.

I made a little example code to show:

for (var i = 0; i <= 100; i++)
{
   using (var engine = new V8ScriptEngine(flags))
   {
       engine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP");
       engine.AddHostType("Console", typeof(Console));
   
       Console.WriteLine("executing script");
   
       
       engine.Execute($@"  
           Console.WriteLine(""Sample Http Request with XMLHTTP"");
       
           function get(url) {{
               var xhr = new XMLHttpRequest();
               xhr.open('GET', url, false);
       
               xhr.send();
               if (xhr.status == 200)
                   return xhr.responseText;
               throw new Error('Request failed: ' + xhr.status);
           }}                    
           Console.WriteLine(get(""https://viacep.com.br/ws/01001000/json/""));
           delete xhr;
       ");                   
        
        // Attempt to release the com      
       engine.CollectGarbage(true);
       engine.Dispose();
   
       GC.Collect();
       GC.WaitForPendingFinalizers();
   }

With the program running it is possible to monitor and see the objects being added, causing the memory problem.

MSXML2

I was using MSXML2.XMLHTTP, to have a single script to test in the browser or in my application. That's how I came across the problem.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions