problem with IHTMLElementCollection Release

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
hi,all, I have a snippet of code like this:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; void MyApp::ProcessElem(MSHTML::IHTMLElementPtr &pElem)
{
HRESULT hr;
<span style="color:Blue; long lLength = 0;
IDispatchPtr pElemDisp = NULL;
MSHTML::IHTMLElementCollectionPtr pChElemColl = NULL;
MSXML2::IXMLDOMElementPtr pXMLDOMElem = NULL;
MSHTML::IHTMLImgElementPtr pImgElem = NULL;
MSHTML::IHTMLAnchorElementPtr pAnchElem = NULL;

hr = pElem->get_children(&pElemDisp);
<span style="color:Blue; if(SUCCEEDED(hr))
{
hr = pElemDisp->QueryInterface(IID_IHTMLElementCollection, (VOID**)&pChElemColl);
<span style="color:Blue; if(SUCCEEDED(hr))
{
hr = pChElemColl->get_length(&lLength);
<span style="color:Blue; if(SUCCEEDED(hr))
{
<span style="color:Blue; for(<span style="color:Blue; int i = 0; i < lLength; i++)
{
VARIANT varIndex;
varIndex.vt = VT_UINT;
varIndex.lVal = i;
VARIANT var2;
VariantInit(&var2);
IDispatchPtr pDisp = NULL;

hr = pChElemColl->raw_item(var2, varIndex, &pDisp);
<span style="color:Blue; if(hr == S_OK)
{
MSHTML::IHTMLElementPtr pChElem = NULL;
hr = pDisp->QueryInterface(IID_IHTMLElement, (<span style="color:Blue; void **)&pChElem);
<span style="color:Blue; if(hr == S_OK)
{
ProcessElem(pChElem, pXMLDoc, pXMLDOMElem);
pChElem->Release();
}
}
}
}
pChElemColl->Release();
}
}
}
[/code]

when the lLength is 0, the program crashed at

<div style="color:Black;background-color:White; <pre>
pChElemColl->Release();
[/code]

so whats with my code?
cheers
daiyue

View the full article
 
Back
Top