PRB: Detach Method of Binary Behavior Is Never Called (259016)



The information in this article applies to:

  • Microsoft Internet Explorer (Programming) 5
  • Microsoft Internet Explorer (Programming) 5.01
  • Microsoft Internet Explorer (Programming) 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.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. 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;
    						
  2. 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;
    }
    						
  3. 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;
    }
    						
  4. Define a breakpoint in the Detach method.
  5. Create an HTML page that uses your binary behavior.
  6. Open Internet Explorer from the debugger, and then load the HTML page that you created.
  7. Open a new HTML page, and then close the browser.
In both of these cases, you will not see the Detach method on your binary behavior called.

REFERENCES

Please view the following Web site for more information:

For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

Modification Type:MajorLast Reviewed:5/11/2006
Keywords:kbDHTML kbprb KB259016