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.
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: | Major | Last Reviewed: | 5/11/2006 |
---|
Keywords: | kbDHTML kbprb KB259016 |
---|
|