Disposing underlying object from finalizer in an immutable object
Posted
by
Juan Luis Soldi
on Stack Overflow
See other posts from Stack Overflow
or by Juan Luis Soldi
Published on 2012-07-04T02:58:41Z
Indexed on
2012/07/04
3:15 UTC
Read the original article
Hit count: 317
I'm trying to wrap around Awesomium
and make it look to the rest of my code as close as possible to NET's WebBrowser
since this is for an existing application that already uses the WebBrowser
.
In this library, there is a class called JSObject
which represents a javascript object. You can get one of this, for instance, by calling the ExecuteJavascriptWithResult
method of the WebView
class. If you'd call it like myWebView.ExecuteJavascriptWithResult("document", string.Empty).ToObject()
, then you'd get a JSObject
that represents the document.
I'm writing an immutable class (it's only field is a readonly JSObject
object) called JSObjectWrap
that wraps around JSObject
which I want to use as base class for other classes that would emulate .NET classes such as HtmlElement
and HtmlDocument
. Now, these classes don't implement Dispose
, but JSObject
does. What I first thought was to call the underlying JSObject
's Dispose
method in my JSObjectWrap
's finalizer (instead of having JSObjectWrap
implement Dispose
) so that the rest of my code can stay the way it is (instead of having to add using
's everywhere and make sure every JSObjectWrap
is being properly disposed).
But I just realized if more than two JSObjectWrap
's have the same underlying JSObject
and one of them gets finalized this will mess up the other JSObjectWrap
. So now I'm thinking maybe I should keep a static Dictionary
of JSObjects
and keep count of how many of each of them are being referenced by a JSObjectWrap
but this sounds messy and I think could cause major performance issues.
Since this sounds to me like a common pattern I wonder if anyone else has a better idea.
© Stack Overflow or respective owner