Should I delete instances of HtmlElement?

In WinForms, the WebBrowser has a Document property of type HtmlDocument . HtmlDocument instance has properties / methods such as Forms , Links , GetElementsByTagName() , etc., which return HtmlElementCollection instances. When I HtmlElementCollection over the HtmlElementCollection , I get instances of the HtmlElement . These HtmlElement instances have HtmlElement properties, which are a reference to the underlying COM object. My question is, should I call the Marshal.ReleaseComObject() method for these HtmlElement instances HtmlElement or HtmlElement WinForms handle internal links?

+4
source share
2 answers

Manual memory management is a bad idea, especially for COM objects. You can get an opinion on this from experts; a blog post from the Visual Studio team brings the point home pretty well.

Just in case, you still think this is a good idea, the Winforms team has already made a decision for you. An interface pointer wrapped by classes such as HtmlDocument, HtmlElement, HtmlWindow, HtmlElementCollection, etc., is a private variable of the class. You simply cannot get to it without breaking all the rules in the book.

It’s not entirely impossible to have a problem, these objects of the shell class are quite small, so you may have a problem with the garbage collector, which does not work often enough to free the original COM objects. GC.Collect () is a reserve for this. Use it only when necessary.

+4
source

If you do not call the ReleaseComObject function, the objects will be automatically released by winforms. Msdn says that it can be used to control the lifetime of an object, but this is not necessary.

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx

+1
source

Source: https://habr.com/ru/post/1489675/


All Articles