<p>I'm running into a perplexing problem with an ActiveX control I'm writing - sometimes, Internet Explorer appears to fail to properly unload the control on process shutdown. This results in the control instance's destructor not being called.</p>

<p>The control is written in C++, uses ATL and it's compiled using Visual Studio 2005. The control instance's destructor is always called when the user browses away from the page the control is embedded in - the problem only occurs when the browser is closed. </p>

<p>When I run IE under a debugger, I don't see anything unusual - the debugger doesn't catch any exceptions, access violations or assertion failures, but the problem is still there - I can set a breakpoint in the control's destructor and it's never hit when I close the broswer.</p>

<p>In addition, when I load a simple HTML page that embeds multiple instances of the control I don't see the problem. The problem only appears to happen when the control is instantiated from our web application, which inserts  tags dynamically into the web page - of course, not knowing what causes this problem, I don't know whether this bit of information is relevant or not, but it does seem to indicate that this might be an IE problem, since it's data dependent. </p>

<p>When I run the simple test case under the debugger, I can set a breakpoint in the control's destructor and it's hit every time. I believe this rules out a problem with the control itself (say, an error that would prevent the destructor from ever being called, like an interface leak.)</p>

<p>I do most of my testing with IE 6, but I've seen the problem occur on IE 7, as well. I haven't tested IE 8.</p>

<p>My working hypothesis right now is that there's something in the dynamic HTML code that causes the browser to leak an interface on the ActiveX control. So far, I haven't been able to produce a good test case that reproduces this outside of the application, and the application is a bit too large to make a good test case.</p>

<p>I was hoping that someone might be able to provide insight into possible IE bugs that are known to cause this kind of behavior. The answer provided below, by the way, is too general - I'm looking for a specific set of circumstances that is known to cause this. Surely someone out there has seen this before.</p>

[{'answer_id': 69131, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>To debug a problem in COM with C++ where an object's (C++) destructor is not being called, the best approach is to focus on how the COM object's refcounts are being incremented or decremented.  What is probably happening is that somebody is incrementing the refcount one too many times, and then not decrementing it the same number of times.  This leads to the object not being freed.</p>\n\n<p>It is possible that your dynamic HTML is simply showing up a bug in IE, which doesn't happen if you use a static page.</p>\n\n<p>If there is a bug in IE, the trick would be to figure out what causes the bug to appear, and what you can do to trick IE into releasing your COM object properly (like, making the HTML go away).</p>\n"}, {'answer_id': 165477, 'author': 'Bruce', 'author_id': 6310, 'author_profile': 'https://Stackoverflow.com/users/6310', 'pm_score': 0, 'selected': False, 'text': '<p>Another approach - add cleanup code to your <a href="http://msdn.microsoft.com/en-us/library/ms682583.aspx" rel="nofollow noreferrer">DllMain</a> function (adding that function if it doesn\'t already exist).  Then regardless of reference counts (and reference count errors), when your DLL is unloaded you can clean yourself up:</p>\n\n<pre><code>BOOL WINAPI DllMain(HINSTANCE, DWORD dwReason, LPVOID) {\n    if (dwReason == DLL_PROCESS_DETACH) {\n        CleanUpAnyObjectsStillAlive();\n    }\n}\n</code></pre>\n\n<p>Oh, and a word of warning - don\'t take too long doing your cleanup - if you do, I can\'t promise the process shutdown won\'t kill you anyway.</p>\n'}, {'answer_id': 400201, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I have the same problem, but only on a specific computer.\nThis computer also has a problem with the Flash ActiveX, that remains alive after closing the tab. \nMy guess is that the problem is not with your code. Do you have that problem on other computers?</p>\n'}]
['https://Stackoverflow.com/questions/65133', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9047/']