microsoft/ClearScript

Cannot access a disposed object

gjvdkamp opened this issue · 3 comments

Hi,

I have a V8 scriptengine that I use in a REPL like environment, so I'll leave it open and the user can issue incremental statements to it. Every now and then I get 'Cannot access a disposed object' exceptions when executing code, but I still have a reference to the engine so it shouldn't be cleaned up. But I suppose somethng else internally does get cleaned up. It also happens when I have a reference to a script object that I want to do something with after a while, suspect the same cause.

I couldn't find any settings regarding to garbage collection, do you have any idea how I can fix this? Ie have the whole engine stay as it is and not get cleaned up?

Here's the stacktrace:

Object name: 'Microsoft.ClearScript.V8.V8ScriptEngine'. at Microsoft.ClearScript.V8.V8ScriptEngine.VerifyNotDisposed() at Microsoft.ClearScript.V8.V8ScriptEngine.Execute(UniqueDocumentInfo documentInfo, String code, Boolean evaluate) at Microsoft.ClearScript.ScriptEngine.Evaluate(String code) at XLConnect.AddIn.TaskpanesAndForms.JavascriptWorkbench.AddModule(String moduleDef, String nodeName) in C:\Users\Gert-JanvanderKamp\source\repos\XlConnect\XlConnect.Add-In\JS\JavascriptWorkbench.cs:line 525 at XLConnect.AddIn.TaskpanesAndForms.JavascriptWorkbench.tsHTTP_Click(Object sender, EventArgs e) in C:\Users\Gert-JanvanderKamp\source\repos\XlConnect\XlConnect.Add-In\JS\JavascriptWorkbench.cs:line 536 at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Hello @gjvdkamp,

Every now and then I get 'Cannot access a disposed object' exceptions when executing code, but I still have a reference to the engine so it shouldn't be cleaned up.

The exception indicates that the script engine had been disposed. Object disposal is unrelated to reference tracking and garbage collection. It's normal for a disposed object to remain strongly referenced and therefore not subject to garbage collection, but such an object is typically no longer usable.

You dispose an object by calling its IDisposable.Dispose method, either explicitly or via the using statement or declaration. Is it possible that your script engine is sometimes being disposed before your tsHTTP_Click event is triggered?

Thanks!

Please reopen this issue if you have additional information or findings related to this topic. Thank you!

Hi apologies for the late reply. I don't have any direct calls to Dispose() on the engine, I suspect one of the references to engine objects make a Dispose call that triggers stuff, need to rework that to use WeakReferences anyway to prevent memory leaks. No further input required thanks for the reply.