Microsoft KB Archive/259016

= PRB: Detach Method of Binary Behavior Is Never Called =

Article ID: 259016

Article Last Modified on 5/11/2006

-

APPLIES TO


 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.01
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q259016



SYMPTOMS
The Detach method of the IElementBehavior interface is called on a binary behavior when it is being detached from the HTML element to which it is bound. Developers that write their own binary behaviors may see that the Detach method is never called on their binary behavior, causing the resources not to free and the binary behavior to remain in memory.



CAUSE
This problem occurs because the binary behavior is maintaining a reference to the attached element as a member variable of one of the binary behavior's C++ classes. This causes Microsoft Internet Explorer to behave as if the HTML element is still in use, and causes the binary behavior to remain attached.



RESOLUTION
To resolve this problem, maintain a pointer to the IElementBehaviorSite interface handed to the IElementBehavior::Init method when the binary behavior is attached, and then obtain the pointer to the attached element on an as-needed basis by calling the IElementBehaviorSite::GetElement method. Release the IHTMLElement pointer as soon as your event handling is complete.

As an example, sink the IHTMLElementEvents interface for the attached element, and then perform some processing every time the user clicks on the element. If the sink is implemented on the same C++ class as the binary behavior, the OnClick handler for the sink looks like the following:

// Class member IElementBehaviorSite *pSite;

HRESULT Init(IElementBehaviorSite *pSiteInit) { pSite = pSiteInit; pSite->AddRef;

return S_OK; }

void OnClick(void) { IHTMLElement *pElem = NULL; HRESULT hr = S_OK;

hr = pSite->GetElement(&pElem); if (SUCCEEDED(hr)) { // Do something with the attached element. }

(void)pElem->Release; pElem = NULL; }

This behavior is by design.



Steps to Reproduce the Behavior
  Define a member variable for the attached element in your C++ class representing your binary behavior. The following is an example of defining a member variable: IHTMLElement *pElem;   In your binary behavior's IElementBehavior::Init method, cache a reference to the attached element. The following is an example of how to cache a reference to an attached element: HRESULT Init(IElementBehaviorSite *pSiteInit) { HRESULT hr = S_OK;

pSite = pSiteInit; pSite->AddRef; hr = pSite->GetElement(&pElem); if (FAILED(hr)) { pSite = NULL; }

return S_OK; }                         Implement the IElementBehavior::Detach method, freeing all class resources. The following is an example of freeing all class resources: HRESULT Detach { if (pSite) { (void)pSite->Release; pSite = NULL; }   if (pElem) { (void)pElem->Release; pElem = NULL; }

return S_OK; }                        Define a breakpoint in the Detach method. Create an HTML page that uses your binary behavior. Open Internet Explorer from the debugger, and then load the HTML page that you created.</li> Open a new HTML page, and then close the browser.</li></ol>

In both of these cases, you will not see the Detach method on your binary behavior called.

<div class="references_section">