how to release pointers in a function

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
hi,all, I use some pointers to handle html doc and html elements in a function like below:

<div style="color:Black;background-color:White; <pre>
hash_map<wstring, wstring> CMyAppVCDlg::GetNodeStyles(VARIANT varSrc)
{
<span style="color:Blue; long lLength = 0;
IHTMLDocument2 *htmDoc = NULL;
IHTMLElementCollection* pElemColl = NULL;
IDispatch *ppvdisp = NULL;
IHTMLElement* pChElem = NULL;
IHTMLStyle* pStyle = NULL;

_bstr_t bstrtTagName;
BSTR bstrTagName = NULL;
BSTR bstrFtStyle = NULL;
BSTR bstrFtFam = NULL;
BSTR txtDecoration = NULL;
hash_map<wstring, wstring> hmStyles;

SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (<span style="color:Blue; void**) &htmDoc);

VARIANT *param = NULL;
HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
param->vt = VT_BSTR;
param->bstrVal = varSrc.bstrVal;

hr = SafeArrayUnaccessData(psaStrings);
hr = htmDoc->write(psaStrings);

hr = htmDoc->get_all(&pElemColl);
hr = pElemColl->get_length(&lLength);
<span style="color:Blue; for(<span style="color:Blue; long i = 0; i < lLength; i++)
{
_variant_t name(i);
_variant_t index(i);

hr = pElemColl->item(name, index, &ppvdisp);
<span style="color:Blue; if(ppvdisp && SUCCEEDED(hr))
{
hr = ppvdisp->QueryInterface(IID_IHTMLElement, (<span style="color:Blue; void **)&pChElem);

<span style="color:Blue; if(pChElem && SUCCEEDED(hr))
{
pChElem->get_tagName(&bstrTagName);
hr = pChElem->get_style(&pStyle);
<span style="color:Blue; if(pStyle && SUCCEEDED(hr))
{
hr = pStyle->get_fontStyle(&bstrFtStyle);
<span style="color:Blue; if(bstrFtStyle)
hmStyles[wstring(bstrFtStyle)] = L<span style="color:#A31515; "FontStyle";
hr = pStyle->get_fontFamily(&bstrFtFam);
<span style="color:Blue; if(bstrFtFam)
hmStyles[wstring(bstrFtFam)] = L<span style="color:#A31515; "FontFamily";
hr = pStyle->get_textDecoration(&txtDecoration);
<span style="color:Blue; if(txtDecoration)
hmStyles[wstring(txtDecoration)] = L<span style="color:#A31515; "TextDecoration";
}
}
}

}

<span style="color:Blue; return hmStyles;
}
[/code]

so i guess i need to release memory that are previously allocated for *htmDoc and *pElemColl and so on, but t<span style="border-collapse:collapse; font-family:verdana,arial,helvetica,sans-serif; font-size:12px he value passed as argument to delete must
be either a pointer to a memory block previously allocated with <tt>new</tt>, or a null pointer. so I wonder how to release the pointers, coz I think I may have a memroy leak problem here.
<span style="border-collapse:collapse; font-family:verdana,arial,helvetica,sans-serif; font-size:12px cheers
daiyue


View the full article
 
Back
Top