HtmlEditor :  Phorum 5 The fastest message board... ever.

This is the discussion forum for the HtmlEditor. See also the HtmlEditor home page, where you can download the control, and the Documentation Wiki, a collaborative project for documenting the control.

Goto Thread: PreviousNext
Goto: Forum ListMessage ListNew TopicSearchLog In
HtmlEvent Memory leak and a fix
Posted by: Tim Anderson (
Date: Monday, 21-Apr-2003, 17:10:16

In testing our app for memory leaks I found a significant issue with the htmlEditor. If you have an htmlEditor on a form it never gets disposed (in Net FX 1.1 - not tested 1.0).

The reason is that setting the control as the event handler for COM events (in SetHTMLEvents method, HtmlEditor.cs) creates a COM reference that never gets released.

The following amended CleanupControl method seems to fix the problem, let me know of any problems.

public void CleanupControl()
if (IsCreated)
if (cookie != -1)
cookie = -1;

//new code here to release COM reference to control
if (m_htmldoc != null)
IntPtr ptr = Marshal.GetIDispatchForObject(this);
int i = Marshal.Release(ptr);
while (i > 0)
i = Marshal.Release(ptr);

site = null;
this.m_htmldoc = null;

Re: HtmlEvent Memory leak and a fix
Posted by: James Hancock (
Date: Monday, 21-Apr-2003, 19:16:06

Sorry Tim, this seems to cause a runtime "no such interface supported"

Commenting it out fixes it again.

Re: HtmlEvent Memory leak and a fix
Posted by: Tim Anderson (
Date: Monday, 21-Apr-2003, 22:22:37

I'm surprised because it runs fine here, even in Win 98. Did you definitely put this in HtmlEditor.cs? Is it the call to Marshal.GetIDispatchForObject that raises the error?

Having said that, I've changed the code a bit because there are circumstances when CleanupControl doesn't get called. I am on the right lines I think. To demo the issue, I've appended the results from the sample app I did to test grin's memory issue. The app calls showdialog in a loop, using a form on which there is an htmlEditor. The form loads a document and then closes. The loop runs up to 1000.

Note that if there is no event handler for HtmlEvent, the results are like the 2nd table, even with the "old" htmlEditor:

Before change
Left column - number of iterations
Right column - memory in bytes reported by GC.GetTotalMemory

0 66112 (before showing htmlEditor at all)
100 265672
200 410292
300 563096
400 715632
500 868208
600 1020720
700 1173184
800 1325648
900 1478308
1000 1632392

After change
0 66112 (before showing htmlEditor at all)
100 214668
200 206488
300 206536
400 206568
500 206648
600 206728
700 206808
800 206888
900 206968
1000 207048

Re: HtmlEvent Memory leak and a fix
Posted by: James Hancock (
Date: Tuesday, 22-Apr-2003, 00:50:52

Definately on the right track!

Yes, it throws the error on the marshal.getdispatch... line.

Yes, I copied it in and overwrote the CleanupControl function in the htmleditor.cs file.

Keep up the good work!

Your Name: 
Your Email: 
Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically.
This is a moderated forum. Your message will remain hidden until it has been approved by a moderator or administrator
This forum powered by Phorum.