Quantcast
Channel: ClearScript
Viewing all 2297 articles
Browse latest View live

Edited Issue: Fatal error in heap setup, Allocation failed - process out of memory [44]

0
0
Hi,

While investigating some memory related issues in my application, I managed to reproduce what seems to be a problem. The following minimal application terminates on my PC after ~50 cycles and prints the following to console:

```
#
# Fatal error in heap setup
# Allocation failed - process out of memory
#
```

```
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 1000; i++)
{
using (V8ScriptEngine engine = new V8ScriptEngine())
{
Console.WriteLine("Created engine #{0}", i);
engine.Execute("function zz(){}");
var tmp = engine.Evaluate("zz");
}
}
}
}
```


Strangely, the memory usage doesn't seem high when it terminates.

I used both a Nuget package, and a manually built latest ClearScript + V8 with same results.

Any help will be greatly appreciated.
Thanks in advance again,
Ron


Edited Issue: ClearScript hangs application due to aggressive assembly loads in AssemblyHelpers.BuildAssemblyTable [84]

0
0
An application I wrote was using ClearScript successfully locally. However, when I deployed the application to one of our application servers, the application would hang on start up. I captured a dump and used WinDbg to find that ClearScript was within AssemblyHelpers.BuildAssemblyTable().

This is a private method that is called within the static constructor of AssemblyHelpers. It gets the .NET framework install directory from the registry, then enumerates every DLL within that directory and all subdirectories and attempts to load it as an assembly.

I cannot imagine why you would need to load literally every single assembly on the machine...

But the main problem with this is that the .NET install directory includes "Temporary ASP.NET Files", which contains assemblies generated from views, templates, etc, and often times copies of their dependencies.

The app server where the hang was occurring has been in production for a long time, and every deployment we make to it ends up creating an additional folder in the Temporary ASP.NET Files directory. There was in fact many gigabytes worth of DLLs in this directory.

So ClearScript spun its wheels trying to enumerate and load over 100,000 DLLs! Out of curiosity I left it running overnight -- it actually finished after several 6 or 7 hours.

I have cleaned up the temp directory, but this isn't a solution. The problem will only come back. Every time we deploy, we're creating more files in that directory, which will progressively slow down the app start up time as ClearScript will have more and more assemblies to load.

Please reconsider the approach. Loading an assembly is not only expensive in terms of time and RAM, it can actually cause negative side effects. I can't imagine why loading assemblies so aggressively is necessary. If it's an issue with discovering 'scriptable' types, consider requiring the caller to provide the list of scriptable types via a call, or to annotate their assembly with the list. Something other than this, pretty please ;) I love ClearScript and would love to continue using it. But I am using this in a business application that is relied upon by thousands of users, and I can't have the potential and unpredictable side effects of random assembly loads, nor the long startup time it's causing.



Edited Issue: Are multiline strings possible? [54]

0
0
I would like to use ClearScript to compile handlebarsjs templates. Handlebars templates can be full html documents that as far as I know there's no good way to build up in js alone ( they have to be passed in via an API or grabbed via an ajax download or `elment.innerHTML`).

Is there a way in ClearScript where I can inject a multiline string containing a document into a variable or am I barking up the wrong tree?

Edited Issue: Issue concatenating java string and c sharp string [55]

0
0
This is in the basic java engine (not v8)

I have some basic code here (in javascript):
```
var accountstr = host.newVar(CLRString);
if (!downloadInfo.TryGetValue("Account", accountstr.out))
{
accountstr = "No Key found";
}
var output = "_" + accountstr;

return output;
```

This should be returning "_908561", but is instead giving me "_undefined". When I remove the underscore concatenation, I get the proper result.

Is there some sort of trick when concatenating a c sharp string and a javascript string?


EDIT:
I managed to find a solution to my problem. For some reason, I have to call .ToString on my System.String. Very weird I had to do this, but it worked.

Edited Feature: Throw specific script exception by host [43]

0
0
Hi,

Would it be possible to add a custom exception, so that when it is thrown by host it throws a script exception of the specified type and message.

For example, in host code:

```
throw new CustomScriptException("RangeError", "custom message");
```

will cause script to execute:

```
throw new RangeError('custom message');
```

Specifically, for the example above I need the exception to be instanceof RangeError.

Thanks again in advance,
Ron


Edited Feature: Support a CommonJS environment for V8 [45]

0
0
To my understanding, ClearScript V8 does not support the [CommonJS](http://stackoverflow.com/questions/16521471/relation-between-commonjs-amd-and-requirejs) standard ([official website](http://www.commonjs.org/)).

My suggestion is to support this.

Alternatively, as you [suggested here](https://clearscript.codeplex.com/workitem/31#PostedByLink1) for the `window` object, is there a way for myself to support the CommonJS API/standard by adding some script and/or host objects?

_(My ultimative goal is to use some 3rd party NodeJS modules inside ClearScript V8 without having to modify their source code to work with ClearScript)_

Edited Issue: Package restore not working [41]

0
0
Add the nuget package for Clearscript V8 to a solution, build solution this completes with no errors.
Remove folder packages\ClearScript.V8.5.3.10.0, build solution this restores and completes with no errors.

remove V8 and replace with Clearscript without V8 dependancies.
Remove the folder packages\ClearScript.5.3.8, now the build solution fails because package restores fails:

Unable to find version '5.3.8' of package 'ClearScript'

Edited Task: What the AccessContext is for and why V8ScriptEngine with name? [22]

0
0
Hello ClearScript,

Two question about object design and purpose:

1.) What the property AccessContext of the V8ScriptEngine is for? It looks promising but I can't see any usage of this at the moment.

2.) Why the V8ScriptEngine Type has got a constructor with name? I can't see that I can use it anytime.

With best regards,
Torsten

Edited Task: Influence the available commands while execution [21]

0
0
Hi ClearScript,

when I execute JavaScript code I have to add objects, types etc. before I call execute. The JavaScript code itself has not influence which objects or types are available.

But what I want is that the JavaScript Code itself has got an option to define what objects / types etc. it needs an the runtime has the option to add it, like following pseudocode

1st. the bootstrap

```
using (var engine = new V8ScriptEngine())
{
engine.AddHostObject("using", MyApp.PackageManager);
... ' something what is additionally need
engine.Execute(myscript);
}
```

2nd, the JavaScript

```
using.AddType("System.Environment");

var username = Enviroment.UserName;
```

3rd, the "MyApp.PackageManager"

```
static void AddType(string name)
{
_ V8ScriptEngine.Current.AddHostType(name);_
}
```

I know, there is no option like V8ScriptEngine.Current at the moment. But is there another way to do this?

Best regards,
Torsten

Edited Task: can this work inside a winRT "metro" C# application? [10]

0
0
can this work inside a winRT "metro" C# application?

Edited Issue: Removing nuget package doesnt remove post build event [40]

0
0
I installed the nuget package and then removed it. Seems to have left behind the post build event command lines. Booo :(

New Post: Could not load file or assembly 'ClearScriptV8-32' or one of its dependencies. An attempt was made to load a program with an incorrect format.

0
0
Hello Subrata!

Sorry, a few things aren't clear from the information you provided:
  1. Is your application 32-bit or 64-bit?
  2. Where are the following assemblies stored, relative to your application's root directory: ClearScriptV8-32.dll, ClearScriptV8-64.dll, v8-ia32.dll, v8-x64.dll?
  3. Have you set the "Copy to Output Directory" property for these files to "Do not copy"?
  4. Are you deploying a debug ClearScript build?
  5. Have you installed Visual C++ Redistributable Packages for Visual Studio 2013?
Also, if you haven't done so, please see Section IV ("Integrating and deploying ClearScript with your application") in the ClearScript ReadMe.

Thanks!

New Post: Could not load file or assembly 'ClearScriptV8-32' or one of its dependencies. An attempt was made to load a program with an incorrect format.

0
0
Hi,
  1. My application is 64 bit and it works fine with ClearScript.dll
  2. ClearScriptV8-64.dll, v8-x64.dll are stored in the application's directory. Which looks correct to me as I stored ClearScript.dll at the same location and it worked.
  3. Yes, I have set Do not copy.
  4. Yeah I am deploying debug build.
  5. I have installed Visual C++ Redistributable Packages for Visual Studio 2013.
    and I have used signed dll for these dlls.
Do you have Microsoft internal distribution list or developer group?

Thanks,
Subrata Biswas

New Post: Could not load file or assembly 'ClearScriptV8-32' or one of its dependencies. An attempt was made to load a program with an incorrect format.

0
0
Hi again,

1.My application is 64 bit and it works fine with ClearScript.dll

Your original message mentions an initial problem loading ClearScriptV8-32, which is a 32-bit assembly. If you're seeing that in a 64-bit app, please make sure that no ClearScript or V8 assemblies appear in your application's bin directory.

ClearScriptV8-64.dll, v8-x64.dll are stored in the application's directory. Which looks correct to me as I stored ClearScript.dll at the same location and it worked.

Your file placement seems correct, but what do you mean "it worked"? Please describe the scenario that isn't working.

Yeah I am deploying debug build.

If you're deploying a debug build of ClearScript, you'll have to install the debug Visual C++ runtime libraries, which are not redistributable. See the directory VC\Redist\Debug_NonRedist in your Visual Studio installation directory.

Do you have Microsoft internal distribution list or developer group?

This is the best place for discussing ClearScript, but you can also send email to ClearScript@microsoft.com.

Cheers!

Created Unassigned: DynamicObject: Unable to invoke method with Int argument [89]

0
0
I've been testing some of the ClearScript functionality based on following versions and an ability to invoke DynamicObject methods with various parameters. My testing didn't go far, I immediately started to see issues with Int based arguments for the methods I invoked. Here is a code I used:

```
Module Module1

Sub Main()
Using engine As New V8ScriptEngine(V8ScriptEngineFlags.DisableGlobalMembers, V8ScriptEngineFlags.EnableDebugging, 9222)
With engine
' test dynamic object
.AddHostObject("testDynamicObj", New TestDynamicObject)
.Execute("testDynamicObj.Run(111);")
End With
End Using
End Sub

End Module

Public Class TestDynamicObject
Inherits DynamicObject

Public Overrides Function GetDynamicMemberNames() As IEnumerable(Of String)
Return New List(Of String) From {"Run"}
End Function

Public Overrides Function TryGetMember(binder As GetMemberBinder, ByRef result As Object) As Boolean
Return False
End Function

Public Overrides Function TryInvokeMember(binder As InvokeMemberBinder, args() As Object, ByRef result As Object) As Boolean
result = binder.Name
Return True
End Function
End Class

```
Here is a test results:
* ClearScript 5.3.10 - worked as expected, I've been able to reach TryInvokeMember an execute it.
* ClearScript 5.4/5.4.2.1 - error, unable to reach TryInvokeMember. Error details as following:

```
Microsoft.ClearScript.ScriptEngineException was unhandled
_HResult=-2146233079
_message=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
HResult=-2146233079
IsTransient=false
Message=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
Source=ClearScriptV8-64
EngineName=2
ErrorDetails=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
at Script Document:1:16 -> testDynamicObj.Run(111);
IsFatal=false
StackTrace:
at V8Exception.ThrowScriptEngineException(V8Exception* )
at Microsoft.ClearScript.V8.V8ContextProxyImpl.Execute(String gcDocumentName, String gcCode, Boolean evaluate, Boolean discard)
at Microsoft.ClearScript.V8.V8ScriptEngine.<>c__DisplayClass1b.<Execute>b__19()
at Microsoft.ClearScript.ScriptEngine.ScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.BaseScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.<>c__DisplayClass25`1.<ScriptInvoke>b__24()
at Microsoft.ClearScript.V8.?A0x361e23e8.InvokeAction(Void* pvActionRef)
at Microsoft.ClearScript.V8.V8ContextProxyImpl.InvokeWithLock(Action gcAction)
at Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.Execute(String documentName, String code, Boolean evaluate, Boolean discard)
at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, Boolean discard, String code)
at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, String code)
at Microsoft.ClearScript.ScriptEngine.Execute(String code)
at ClearScript_5._3._9Test.EngineWrapper.RunScriptsExperimental(ScriptToRun scriptToRun, Int32 operationTimeout) in C:\Users\mshakirov\Documents\Visual Studio 2012\Projects\ClearScript-5.3.9Test\ClearScript-5.3.9Test\Module1.vb:line 113
at ClearScript_5._3._9Test.Module1.Main() in C:\Users\mshakirov\Documents\Visual Studio 2012\Projects\ClearScript-5.3.9Test\ClearScript-5.3.9Test\Module1.vb:line 16
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.InvalidOperationException
_HResult=-2146233079
_message=An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
HResult=-2146233079
IsTransient=false
Message=An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
Source=System.Core
StackTrace:
at System.Linq.Expressions.Expression.NewArrayInit(Type type, IEnumerable`1 initializers)
at Microsoft.ClearScript.Util.DynamicHelpers.DynamicInvokeMemberBinder.FallbackInvoke(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
at System.Dynamic.DynamicObject.MetaDynamic.BuildCallMethodWithResult(String methodName, DynamicMetaObjectBinder binder, Expression[] args, DynamicMetaObject fallbackResult, Fallback fallbackInvoke)
at System.Dynamic.DynamicObject.MetaDynamic.BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
at Microsoft.ClearScript.Util.DynamicHelpers.InvokeMember(DynamicMetaObject target, IHostInvokeContext context, String name, BindingFlags invokeFlags, Object[] args)
at Microsoft.ClearScript.Util.DynamicHelpers.<>c__DisplayClass7.<TryInvokeMember>b__6()
at Microsoft.ClearScript.Util.DynamicHelpers.TryDynamicOperation[T](Func`1 operation, T& result)
at Microsoft.ClearScript.Util.DynamicHelpers.TryInvokeMember(DynamicMetaObject target, IHostInvokeContext context, String name, BindingFlags invokeFlags, Object[] args, Object& result)
at Microsoft.ClearScript.HostItem.InvokeHostMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling)
at Microsoft.ClearScript.HostMethod.TryInvoke(IHostInvokeContext context, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, Object& result)
at Microsoft.ClearScript.Util.InvokeHelpers.TryInvokeObject(IHostInvokeContext context, Object target, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, Boolean tryDynamic, Object& result)
at Microsoft.ClearScript.HostItem.InvokeHostMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.<>c__DisplayClass4b.<InvokeReflectMember>b__4a()
at Microsoft.ClearScript.ScriptEngine.HostInvoke[T](Func`1 func)
at Microsoft.ClearScript.HostItem.HostInvoke[T](Func`1 func)
at Microsoft.ClearScript.HostItem.InvokeReflectMember(String name, BindingFlags invokeFlags, Object[] wrappedArgs, CultureInfo culture, String[] namedParams, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeReflectMember(String name, BindingFlags invokeFlags, Object[] wrappedArgs, CultureInfo culture, String[] namedParams)
at Microsoft.ClearScript.HostItem.System.Reflection.IReflect.InvokeMember(String name, BindingFlags invokeFlags, Binder binder, Object invokeTarget, Object[] wrappedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at Microsoft.ClearScript.HostItem.Microsoft.ClearScript.Util.IDynamic.Invoke(Object[] args, Boolean asConstructor)
at Microsoft.ClearScript.V8.V8ProxyHelpers.InvokeHostObject(Object obj, Object[] args, Boolean asConstructor)
at Microsoft.ClearScript.V8.V8ProxyHelpers.InvokeHostObject(Void* pObject, Object[] args, Boolean asConstructor)
at HostObjectHelpers.Invoke(V8Value* , Void* pvObject, vector<V8Value\,std::allocator<V8Value> >* args, Boolean asConstructor)
InnerException:
```
Just out of curiosity I've tested this with string argument, or non of it at all and it worked just as expected. I didn't test with any other argument type from that point and decided to share this with ClearScript team. Could someone elaborate on this and either point out at problem in the test code or confirm that there is a deeper issue at the ClearScript/V8 levels?

Thanks in advance,
Max


Commented Unassigned: DynamicObject: Unable to invoke method with Int argument [89]

0
0
I've been testing some of the ClearScript functionality based on following versions and an ability to invoke DynamicObject methods with various parameters. My testing didn't go far, I immediately started to see issues with Int based arguments for the methods I invoked. Here is a code I used:

```
Module Module1

Sub Main()
Using engine As New V8ScriptEngine(V8ScriptEngineFlags.DisableGlobalMembers, V8ScriptEngineFlags.EnableDebugging, 9222)
With engine
' test dynamic object
.AddHostObject("testDynamicObj", New TestDynamicObject)
.Execute("testDynamicObj.Run(111);")
End With
End Using
End Sub

End Module

Public Class TestDynamicObject
Inherits DynamicObject

Public Overrides Function GetDynamicMemberNames() As IEnumerable(Of String)
Return New List(Of String) From {"Run"}
End Function

Public Overrides Function TryGetMember(binder As GetMemberBinder, ByRef result As Object) As Boolean
Return False
End Function

Public Overrides Function TryInvokeMember(binder As InvokeMemberBinder, args() As Object, ByRef result As Object) As Boolean
result = binder.Name
Return True
End Function
End Class

```
Here is a test results:
* ClearScript 5.3.10 - worked as expected, I've been able to reach TryInvokeMember an execute it.
* ClearScript 5.4/5.4.2.1 - error, unable to reach TryInvokeMember. Error details as following:

```
Microsoft.ClearScript.ScriptEngineException was unhandled
_HResult=-2146233079
_message=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
HResult=-2146233079
IsTransient=false
Message=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
Source=ClearScriptV8-64
EngineName=2
ErrorDetails=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
at Script Document:1:16 -> testDynamicObj.Run(111);
IsFatal=false
StackTrace:
at V8Exception.ThrowScriptEngineException(V8Exception* )
at Microsoft.ClearScript.V8.V8ContextProxyImpl.Execute(String gcDocumentName, String gcCode, Boolean evaluate, Boolean discard)
at Microsoft.ClearScript.V8.V8ScriptEngine.<>c__DisplayClass1b.<Execute>b__19()
at Microsoft.ClearScript.ScriptEngine.ScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.BaseScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.<>c__DisplayClass25`1.<ScriptInvoke>b__24()
at Microsoft.ClearScript.V8.?A0x361e23e8.InvokeAction(Void* pvActionRef)
at Microsoft.ClearScript.V8.V8ContextProxyImpl.InvokeWithLock(Action gcAction)
at Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.Execute(String documentName, String code, Boolean evaluate, Boolean discard)
at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, Boolean discard, String code)
at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, String code)
at Microsoft.ClearScript.ScriptEngine.Execute(String code)
at ClearScript_5._3._9Test.EngineWrapper.RunScriptsExperimental(ScriptToRun scriptToRun, Int32 operationTimeout) in C:\Users\mshakirov\Documents\Visual Studio 2012\Projects\ClearScript-5.3.9Test\ClearScript-5.3.9Test\Module1.vb:line 113
at ClearScript_5._3._9Test.Module1.Main() in C:\Users\mshakirov\Documents\Visual Studio 2012\Projects\ClearScript-5.3.9Test\ClearScript-5.3.9Test\Module1.vb:line 16
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.InvalidOperationException
_HResult=-2146233079
_message=An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
HResult=-2146233079
IsTransient=false
Message=An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
Source=System.Core
StackTrace:
at System.Linq.Expressions.Expression.NewArrayInit(Type type, IEnumerable`1 initializers)
at Microsoft.ClearScript.Util.DynamicHelpers.DynamicInvokeMemberBinder.FallbackInvoke(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
at System.Dynamic.DynamicObject.MetaDynamic.BuildCallMethodWithResult(String methodName, DynamicMetaObjectBinder binder, Expression[] args, DynamicMetaObject fallbackResult, Fallback fallbackInvoke)
at System.Dynamic.DynamicObject.MetaDynamic.BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
at Microsoft.ClearScript.Util.DynamicHelpers.InvokeMember(DynamicMetaObject target, IHostInvokeContext context, String name, BindingFlags invokeFlags, Object[] args)
at Microsoft.ClearScript.Util.DynamicHelpers.<>c__DisplayClass7.<TryInvokeMember>b__6()
at Microsoft.ClearScript.Util.DynamicHelpers.TryDynamicOperation[T](Func`1 operation, T& result)
at Microsoft.ClearScript.Util.DynamicHelpers.TryInvokeMember(DynamicMetaObject target, IHostInvokeContext context, String name, BindingFlags invokeFlags, Object[] args, Object& result)
at Microsoft.ClearScript.HostItem.InvokeHostMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling)
at Microsoft.ClearScript.HostMethod.TryInvoke(IHostInvokeContext context, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, Object& result)
at Microsoft.ClearScript.Util.InvokeHelpers.TryInvokeObject(IHostInvokeContext context, Object target, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, Boolean tryDynamic, Object& result)
at Microsoft.ClearScript.HostItem.InvokeHostMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.<>c__DisplayClass4b.<InvokeReflectMember>b__4a()
at Microsoft.ClearScript.ScriptEngine.HostInvoke[T](Func`1 func)
at Microsoft.ClearScript.HostItem.HostInvoke[T](Func`1 func)
at Microsoft.ClearScript.HostItem.InvokeReflectMember(String name, BindingFlags invokeFlags, Object[] wrappedArgs, CultureInfo culture, String[] namedParams, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeReflectMember(String name, BindingFlags invokeFlags, Object[] wrappedArgs, CultureInfo culture, String[] namedParams)
at Microsoft.ClearScript.HostItem.System.Reflection.IReflect.InvokeMember(String name, BindingFlags invokeFlags, Binder binder, Object invokeTarget, Object[] wrappedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at Microsoft.ClearScript.HostItem.Microsoft.ClearScript.Util.IDynamic.Invoke(Object[] args, Boolean asConstructor)
at Microsoft.ClearScript.V8.V8ProxyHelpers.InvokeHostObject(Object obj, Object[] args, Boolean asConstructor)
at Microsoft.ClearScript.V8.V8ProxyHelpers.InvokeHostObject(Void* pObject, Object[] args, Boolean asConstructor)
at HostObjectHelpers.Invoke(V8Value* , Void* pvObject, vector<V8Value\,std::allocator<V8Value> >* args, Boolean asConstructor)
InnerException:
```
Just out of curiosity I've tested this with string argument, or non of it at all and it worked just as expected. I didn't test with any other argument type from that point and decided to share this with ClearScript team. Could someone elaborate on this and either point out at problem in the test code or confirm that there is a deeper issue at the ClearScript/V8 levels?

Thanks in advance,
Max

Comments: Hi Max, This appears to be a bug in ClearScript, affecting arguments of value types. Thanks for reporting it! Oddly, you might be able to work around it by removing the no-op TryGetMember() override, or by explicitly casting the argument to `Object` via `HostFunctions.cast()`. Thanks again!

Edited Issue: [BUG] DynamicObject: Unable to invoke method with Int argument [89]

0
0
I've been testing some of the ClearScript functionality based on following versions and an ability to invoke DynamicObject methods with various parameters. My testing didn't go far, I immediately started to see issues with Int based arguments for the methods I invoked. Here is a code I used:

```
Module Module1

Sub Main()
Using engine As New V8ScriptEngine(V8ScriptEngineFlags.DisableGlobalMembers, V8ScriptEngineFlags.EnableDebugging, 9222)
With engine
' test dynamic object
.AddHostObject("testDynamicObj", New TestDynamicObject)
.Execute("testDynamicObj.Run(111);")
End With
End Using
End Sub

End Module

Public Class TestDynamicObject
Inherits DynamicObject

Public Overrides Function GetDynamicMemberNames() As IEnumerable(Of String)
Return New List(Of String) From {"Run"}
End Function

Public Overrides Function TryGetMember(binder As GetMemberBinder, ByRef result As Object) As Boolean
Return False
End Function

Public Overrides Function TryInvokeMember(binder As InvokeMemberBinder, args() As Object, ByRef result As Object) As Boolean
result = binder.Name
Return True
End Function
End Class

```
Here is a test results:
* ClearScript 5.3.10 - worked as expected, I've been able to reach TryInvokeMember an execute it.
* ClearScript 5.4/5.4.2.1 - error, unable to reach TryInvokeMember. Error details as following:

```
Microsoft.ClearScript.ScriptEngineException was unhandled
_HResult=-2146233079
_message=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
HResult=-2146233079
IsTransient=false
Message=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
Source=ClearScriptV8-64
EngineName=2
ErrorDetails=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
at Script Document:1:16 -> testDynamicObj.Run(111);
IsFatal=false
StackTrace:
at V8Exception.ThrowScriptEngineException(V8Exception* )
at Microsoft.ClearScript.V8.V8ContextProxyImpl.Execute(String gcDocumentName, String gcCode, Boolean evaluate, Boolean discard)
at Microsoft.ClearScript.V8.V8ScriptEngine.<>c__DisplayClass1b.<Execute>b__19()
at Microsoft.ClearScript.ScriptEngine.ScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.BaseScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.<>c__DisplayClass25`1.<ScriptInvoke>b__24()
at Microsoft.ClearScript.V8.?A0x361e23e8.InvokeAction(Void* pvActionRef)
at Microsoft.ClearScript.V8.V8ContextProxyImpl.InvokeWithLock(Action gcAction)
at Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.Execute(String documentName, String code, Boolean evaluate, Boolean discard)
at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, Boolean discard, String code)
at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, String code)
at Microsoft.ClearScript.ScriptEngine.Execute(String code)
at ClearScript_5._3._9Test.EngineWrapper.RunScriptsExperimental(ScriptToRun scriptToRun, Int32 operationTimeout) in C:\Users\mshakirov\Documents\Visual Studio 2012\Projects\ClearScript-5.3.9Test\ClearScript-5.3.9Test\Module1.vb:line 113
at ClearScript_5._3._9Test.Module1.Main() in C:\Users\mshakirov\Documents\Visual Studio 2012\Projects\ClearScript-5.3.9Test\ClearScript-5.3.9Test\Module1.vb:line 16
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.InvalidOperationException
_HResult=-2146233079
_message=An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
HResult=-2146233079
IsTransient=false
Message=An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
Source=System.Core
StackTrace:
at System.Linq.Expressions.Expression.NewArrayInit(Type type, IEnumerable`1 initializers)
at Microsoft.ClearScript.Util.DynamicHelpers.DynamicInvokeMemberBinder.FallbackInvoke(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
at System.Dynamic.DynamicObject.MetaDynamic.BuildCallMethodWithResult(String methodName, DynamicMetaObjectBinder binder, Expression[] args, DynamicMetaObject fallbackResult, Fallback fallbackInvoke)
at System.Dynamic.DynamicObject.MetaDynamic.BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
at Microsoft.ClearScript.Util.DynamicHelpers.InvokeMember(DynamicMetaObject target, IHostInvokeContext context, String name, BindingFlags invokeFlags, Object[] args)
at Microsoft.ClearScript.Util.DynamicHelpers.<>c__DisplayClass7.<TryInvokeMember>b__6()
at Microsoft.ClearScript.Util.DynamicHelpers.TryDynamicOperation[T](Func`1 operation, T& result)
at Microsoft.ClearScript.Util.DynamicHelpers.TryInvokeMember(DynamicMetaObject target, IHostInvokeContext context, String name, BindingFlags invokeFlags, Object[] args, Object& result)
at Microsoft.ClearScript.HostItem.InvokeHostMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling)
at Microsoft.ClearScript.HostMethod.TryInvoke(IHostInvokeContext context, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, Object& result)
at Microsoft.ClearScript.Util.InvokeHelpers.TryInvokeObject(IHostInvokeContext context, Object target, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, Boolean tryDynamic, Object& result)
at Microsoft.ClearScript.HostItem.InvokeHostMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.<>c__DisplayClass4b.<InvokeReflectMember>b__4a()
at Microsoft.ClearScript.ScriptEngine.HostInvoke[T](Func`1 func)
at Microsoft.ClearScript.HostItem.HostInvoke[T](Func`1 func)
at Microsoft.ClearScript.HostItem.InvokeReflectMember(String name, BindingFlags invokeFlags, Object[] wrappedArgs, CultureInfo culture, String[] namedParams, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeReflectMember(String name, BindingFlags invokeFlags, Object[] wrappedArgs, CultureInfo culture, String[] namedParams)
at Microsoft.ClearScript.HostItem.System.Reflection.IReflect.InvokeMember(String name, BindingFlags invokeFlags, Binder binder, Object invokeTarget, Object[] wrappedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at Microsoft.ClearScript.HostItem.Microsoft.ClearScript.Util.IDynamic.Invoke(Object[] args, Boolean asConstructor)
at Microsoft.ClearScript.V8.V8ProxyHelpers.InvokeHostObject(Object obj, Object[] args, Boolean asConstructor)
at Microsoft.ClearScript.V8.V8ProxyHelpers.InvokeHostObject(Void* pObject, Object[] args, Boolean asConstructor)
at HostObjectHelpers.Invoke(V8Value* , Void* pvObject, vector<V8Value\,std::allocator<V8Value> >* args, Boolean asConstructor)
InnerException:
```
Just out of curiosity I've tested this with string argument, or non of it at all and it worked just as expected. I didn't test with any other argument type from that point and decided to share this with ClearScript team. Could someone elaborate on this and either point out at problem in the test code or confirm that there is a deeper issue at the ClearScript/V8 levels?

Thanks in advance,
Max

Commented Issue: [BUG] DynamicObject: Unable to invoke method with Int argument [89]

0
0
I've been testing some of the ClearScript functionality based on following versions and an ability to invoke DynamicObject methods with various parameters. My testing didn't go far, I immediately started to see issues with Int based arguments for the methods I invoked. Here is a code I used:

```
Module Module1

Sub Main()
Using engine As New V8ScriptEngine(V8ScriptEngineFlags.DisableGlobalMembers, V8ScriptEngineFlags.EnableDebugging, 9222)
With engine
' test dynamic object
.AddHostObject("testDynamicObj", New TestDynamicObject)
.Execute("testDynamicObj.Run(111);")
End With
End Using
End Sub

End Module

Public Class TestDynamicObject
Inherits DynamicObject

Public Overrides Function GetDynamicMemberNames() As IEnumerable(Of String)
Return New List(Of String) From {"Run"}
End Function

Public Overrides Function TryGetMember(binder As GetMemberBinder, ByRef result As Object) As Boolean
Return False
End Function

Public Overrides Function TryInvokeMember(binder As InvokeMemberBinder, args() As Object, ByRef result As Object) As Boolean
result = binder.Name
Return True
End Function
End Class

```
Here is a test results:
* ClearScript 5.3.10 - worked as expected, I've been able to reach TryInvokeMember an execute it.
* ClearScript 5.4/5.4.2.1 - error, unable to reach TryInvokeMember. Error details as following:

```
Microsoft.ClearScript.ScriptEngineException was unhandled
_HResult=-2146233079
_message=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
HResult=-2146233079
IsTransient=false
Message=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
Source=ClearScriptV8-64
EngineName=2
ErrorDetails=Error: An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
at Script Document:1:16 -> testDynamicObj.Run(111);
IsFatal=false
StackTrace:
at V8Exception.ThrowScriptEngineException(V8Exception* )
at Microsoft.ClearScript.V8.V8ContextProxyImpl.Execute(String gcDocumentName, String gcCode, Boolean evaluate, Boolean discard)
at Microsoft.ClearScript.V8.V8ScriptEngine.<>c__DisplayClass1b.<Execute>b__19()
at Microsoft.ClearScript.ScriptEngine.ScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.BaseScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.<>c__DisplayClass25`1.<ScriptInvoke>b__24()
at Microsoft.ClearScript.V8.?A0x361e23e8.InvokeAction(Void* pvActionRef)
at Microsoft.ClearScript.V8.V8ContextProxyImpl.InvokeWithLock(Action gcAction)
at Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke[T](Func`1 func)
at Microsoft.ClearScript.V8.V8ScriptEngine.Execute(String documentName, String code, Boolean evaluate, Boolean discard)
at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, Boolean discard, String code)
at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, String code)
at Microsoft.ClearScript.ScriptEngine.Execute(String code)
at ClearScript_5._3._9Test.EngineWrapper.RunScriptsExperimental(ScriptToRun scriptToRun, Int32 operationTimeout) in C:\Users\mshakirov\Documents\Visual Studio 2012\Projects\ClearScript-5.3.9Test\ClearScript-5.3.9Test\Module1.vb:line 113
at ClearScript_5._3._9Test.Module1.Main() in C:\Users\mshakirov\Documents\Visual Studio 2012\Projects\ClearScript-5.3.9Test\ClearScript-5.3.9Test\Module1.vb:line 16
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.InvalidOperationException
_HResult=-2146233079
_message=An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
HResult=-2146233079
IsTransient=false
Message=An expression of type 'System.Int32' cannot be used to initialize an array of type 'System.Object'
Source=System.Core
StackTrace:
at System.Linq.Expressions.Expression.NewArrayInit(Type type, IEnumerable`1 initializers)
at Microsoft.ClearScript.Util.DynamicHelpers.DynamicInvokeMemberBinder.FallbackInvoke(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
at System.Dynamic.DynamicObject.MetaDynamic.BuildCallMethodWithResult(String methodName, DynamicMetaObjectBinder binder, Expression[] args, DynamicMetaObject fallbackResult, Fallback fallbackInvoke)
at System.Dynamic.DynamicObject.MetaDynamic.BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
at Microsoft.ClearScript.Util.DynamicHelpers.InvokeMember(DynamicMetaObject target, IHostInvokeContext context, String name, BindingFlags invokeFlags, Object[] args)
at Microsoft.ClearScript.Util.DynamicHelpers.<>c__DisplayClass7.<TryInvokeMember>b__6()
at Microsoft.ClearScript.Util.DynamicHelpers.TryDynamicOperation[T](Func`1 operation, T& result)
at Microsoft.ClearScript.Util.DynamicHelpers.TryInvokeMember(DynamicMetaObject target, IHostInvokeContext context, String name, BindingFlags invokeFlags, Object[] args, Object& result)
at Microsoft.ClearScript.HostItem.InvokeHostMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling)
at Microsoft.ClearScript.HostMethod.TryInvoke(IHostInvokeContext context, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, Object& result)
at Microsoft.ClearScript.Util.InvokeHelpers.TryInvokeObject(IHostInvokeContext context, Object target, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, Boolean tryDynamic, Object& result)
at Microsoft.ClearScript.HostItem.InvokeHostMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.<>c__DisplayClass4b.<InvokeReflectMember>b__4a()
at Microsoft.ClearScript.ScriptEngine.HostInvoke[T](Func`1 func)
at Microsoft.ClearScript.HostItem.HostInvoke[T](Func`1 func)
at Microsoft.ClearScript.HostItem.InvokeReflectMember(String name, BindingFlags invokeFlags, Object[] wrappedArgs, CultureInfo culture, String[] namedParams, Boolean& isCacheable)
at Microsoft.ClearScript.HostItem.InvokeReflectMember(String name, BindingFlags invokeFlags, Object[] wrappedArgs, CultureInfo culture, String[] namedParams)
at Microsoft.ClearScript.HostItem.System.Reflection.IReflect.InvokeMember(String name, BindingFlags invokeFlags, Binder binder, Object invokeTarget, Object[] wrappedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at Microsoft.ClearScript.HostItem.Microsoft.ClearScript.Util.IDynamic.Invoke(Object[] args, Boolean asConstructor)
at Microsoft.ClearScript.V8.V8ProxyHelpers.InvokeHostObject(Object obj, Object[] args, Boolean asConstructor)
at Microsoft.ClearScript.V8.V8ProxyHelpers.InvokeHostObject(Void* pObject, Object[] args, Boolean asConstructor)
at HostObjectHelpers.Invoke(V8Value* , Void* pvObject, vector<V8Value\,std::allocator<V8Value> >* args, Boolean asConstructor)
InnerException:
```
Just out of curiosity I've tested this with string argument, or non of it at all and it worked just as expected. I didn't test with any other argument type from that point and decided to share this with ClearScript team. Could someone elaborate on this and either point out at problem in the test code or confirm that there is a deeper issue at the ClearScript/V8 levels?

Thanks in advance,
Max

Comments: Thank you for a reply...I'll stick to a second workaround for time being and hopefully this all could be fixed in the next release. Max

New Post: MemoryLeak despite disposing of V8 Script Engines

0
0
Greetings! I could really use some help figuring out how to handle this. I've spent a lot of time trying to isolate the problem. I suspect I have multiple problems, actually.

My scenario is what I need to have a user-defined script hook into an event I expose. The event can be raised at any time in the future, so it's going to be a long-running script that sits around. I pass the event a managed object (a kind of event args) that contains information about the event.

The memory usage was continuously climbing as events were raised.

Reading other discussions here, I gather that passing managed objects to scripts can cause some tricky memory management issues. You suggested disposing of them when they are done being used. I can't really do that easily, though. The object that is passed to script contains methods that the script can use to get access to even more objects. To be able to clean them all up, I'd have to have an event-scoped list of objects that were passed in so that I could easily dispose them later, which would be possible, but difficult.

So instead of doing that, I keep track of how often I call the event. After every 100 calls, or so, I will completely dispose of the V8 Engine and the script, recompile it, and re-evalulate it. I figured whatever memory use issues I have should be covered by that, so no need to dispose the host objects directly. Or so I thought.

Even with that, memory continues to climb. I've created a reproduction app to demonstrate:
    class Program
    {
        static void Main(string[] args)
        {
            for (var i = 0; i < 50; i++)
            {
                Console.WriteLine("Iteration " + i + ". Memory = " + Process.GetCurrentProcess().PrivateMemorySize64.ToString("n0"));

                var engine = new Microsoft.ClearScript.V8.V8ScriptEngine();
                var host = new HostObject();
                engine.AddHostObject("host", host);

                var compiledScript = engine.Compile(@"host.onEvent(function() {});");
                engine.Execute(compiledScript);

                for (var j = 0; j < 50000; j++)
                {
                    Raise(host);
                }

                engine.Dispose();
                GC.Collect();
            }

            Console.WriteLine("Done. Memory: " + Process.GetCurrentProcess().PrivateMemorySize64.ToString("n0"));
            Console.ReadLine();
        }

        private static void Raise(HostObject host)
        {
            host.RaiseEvent(new SomeEventArgs());
        }
    }

    public class HostObject
    {
        private dynamic _callback;

        public void onEvent(dynamic callback)
        {
            _callback = callback;
        }

        internal void RaiseEvent(SomeEventArgs args)
        {
            _callback(args);
        }
    }

    public class SomeEventArgs
    {
        public object getThing()
        {
            return new Thing();
        }
    }

    public class Thing
    {
    }
So it's just a loop that continues to raise events. After 50,000 events have been raised, it disposes of the V8 engine and forces GC.Collect. Then it starts over. Each iteration it spits out the private memory use. The output looks like this:
Iteration 0. Memory = 27,127,808
Iteration 1. Memory = 48,844,800
Iteration 2. Memory = 50,040,832
Iteration 3. Memory = 54,665,216
Iteration 4. Memory = 61,140,992
Iteration 5. Memory = 59,023,360
Iteration 6. Memory = 60,551,168
Iteration 7. Memory = 65,290,240
Iteration 8. Memory = 63,279,104
Iteration 9. Memory = 68,558,848
Iteration 10. Memory = 66,580,480
Iteration 11. Memory = 72,167,424
Iteration 12. Memory = 69,926,912
Iteration 13. Memory = 68,534,272
Iteration 14. Memory = 75,706,368
Iteration 15. Memory = 71,839,744
Iteration 16. Memory = 79,609,856
Iteration 17. Memory = 79,757,312
Iteration 18. Memory = 76,496,896
Iteration 19. Memory = 83,943,424
Iteration 20. Memory = 79,884,288
Iteration 21. Memory = 87,781,376
Iteration 22. Memory = 87,670,784
Iteration 23. Memory = 84,738,048
Iteration 24. Memory = 91,852,800
Iteration 25. Memory = 88,100,864
Iteration 26. Memory = 95,801,344
Iteration 27. Memory = 95,752,192
Iteration 28. Memory = 92,495,872
Iteration 29. Memory = 100,003,840
Iteration 30. Memory = 95,817,728
Iteration 31. Memory = 103,780,352
Iteration 32. Memory = 104,042,496
Iteration 33. Memory = 100,737,024
Iteration 34. Memory = 107,982,848
Iteration 35. Memory = 104,165,376
Iteration 36. Memory = 111,931,392
Iteration 37. Memory = 112,066,560
Iteration 38. Memory = 108,818,432
Iteration 39. Memory = 116,264,960
Iteration 40. Memory = 112,078,848
Iteration 41. Memory = 120,107,008
Iteration 42. Memory = 119,910,400
Iteration 43. Memory = 117,051,392
Iteration 44. Memory = 124,166,144
Iteration 45. Memory = 120,414,208
Iteration 46. Memory = 128,114,688
Iteration 47. Memory = 128,196,608
Iteration 48. Memory = 125,067,264
Iteration 49. Memory = 132,448,256
Done. Memory: 128,397,312
Using windbg I looked at the heap. There's a single dictionary in memory that seems to grow forever:

000007fe938e5848 1 1206920 System.Runtime.CompilerServices.ConditionalWeakTable2+Entry[[System.Object, mscorlib],[System.Collections.Generic.List1[[System.WeakReference, mscorlib]], mscorlib]][]

I presume this is some kind of weakreference dictionary that keeps track of proxy objects. It contains 75,000 elements in this example.

0:000> !do 0000000012805c40
Name: System.Runtime.CompilerServices.ConditionalWeakTable2+Entry[[System.Object, mscorlib],[System.Collections.Generic.List1[[System.WeakReference, mscorlib]], mscorlib]][]
MethodTable: 000007fe938e5848
EEClass: 000007fe938e57b8
Size: 1206920(0x126a88) bytes
Array: Rank 1, Number of elements 75431, Type VALUETYPE
Fields:
None

To be fair, while this does seem to reproduce a problem, I'm not even sure that it's my actual problem. I think I have multiple. My actual program is leaking memory a lot faster than I can reproduce with this sample app. That may just be because the host objects I'm passing in are larger though. I could perhaps use some advice or guidelines on what to do or what not to do for the scenario I'm in.

New Post: MemoryLeak despite disposing of V8 Script Engines

Viewing all 2297 articles
Browse latest View live




Latest Images