I have a .NET application in which the user can also input some c# script. The script is executed repeatedly in the application, and, when this happen, I see an handle leak.
I use the static class CSharpScript from Microsoft.CodeAnalysis.CSharp.Scripting. I tried a very simple console application and the problem is still present. Here the code I have used:
using Microsoft.CodeAnalysis.CSharp.Scripting;
while (true)
{
var script = CSharpScript.Create("bool test = true;");
script.Compile();
using (var state = script.RunAsync())
{
var result = state.Result;
}
}
and also:
using Microsoft.CodeAnalysis.CSharp.Scripting;
while (true)
{
using (var state = CSharpScript.RunAsync("bool test = true;"))
{
var result = state.Result;
}
}
I also tried to call the garbage collector in each iteration, but I have the same result. In both the examples the handle is going to the sky: Handle in process monitor
I managed to get a solution for the issue. After some search i finded that EvaluateAsync generates a new assembly in memory and it's impossible to remove.
I have to useCSharpCompilation class instead of CSharpScript to have the same result with no memory leak! It's a little bit more tricky, however it work great. Here a little example: