Does this code leaks memory?
-
Hello, I'm debugging my last program... and today I've acquired Rational Purify (trial version) in order to receive help on this task... after running my program under Rational Purify, I've found some memory leaks... but now I'm not sure of which is my name (almost)... let's see, it says that this code is leaking memory: ------------------------------------------------------------- ------------------------------------------------------------- // Variables que permeten la construcció del codi HTML. CString csCodiHTML = "HTML STRING..."; BSTR bstr; int iWidthAreaClientHTMLCtrl = 0; // Tractament del SafeArray. HRESULT hrTractarAmbSafeArray = S_OK; SAFEARRAY *pSafeArray = NULL; VARIANT *pElement = NULL; extern CTCCApp TCCApp; //------------------------------------------------ // Obtenir accés al control HTML via ATL. //------------------------------------------------ CComQIPtr spHTMLDocument = this->m_HTMLCtrl.GetHtmlDocument(); if(spHTMLDocument) { CComQIPtr spHTML; spHTMLDocument->get_body(&spHTML); if (spHTML) { bstr = csCodiHTML.AllocSysString(); spHTML->put_innerHTML(bstr); spHTML.Release(); SysFreeString(bstr); csCodiHTML.ReleaseBuffer(); csCodiHTML.FreeExtra(); } else { pSafeArray = ::SafeArrayCreateVector(VT_VARIANT, 0, 1); if (pSafeArray) { hrTractarAmbSafeArray = ::SafeArrayAccessData(pSafeArray,(LPVOID*) &pElement); pElement->vt = VT_BSTR; // HERE! pElement->bstrVal = csCodiHTML.AllocSysString(); hrTractarAmbSafeArray = ::SafeArrayUnaccessData(pSafeArray); hrTractarAmbSafeArray = spHTMLDocument->write(pSafeArray); ::SafeArrayDestroy(pSafeArray); csCodiHTML.ReleaseBuffer(); // HERE TOO! csCodiHTML.FreeExtra(); } } } if (spHTMLDocument) spHTMLDocument.Release(); ------------------------------------------------------------- ------------------------------------------------------------- Where you can see HERE! and HERE TOO! is where that program Rational purify is detecting some memory leaks... do you think that the code is leaking memory? and if so... why? I thought that if I have not called new I should'nt call delete and that whenever the function goes out of scope the local variables are destructed automatically... isn't it true? Thank you in advance...
-
Hello, I'm debugging my last program... and today I've acquired Rational Purify (trial version) in order to receive help on this task... after running my program under Rational Purify, I've found some memory leaks... but now I'm not sure of which is my name (almost)... let's see, it says that this code is leaking memory: ------------------------------------------------------------- ------------------------------------------------------------- // Variables que permeten la construcció del codi HTML. CString csCodiHTML = "HTML STRING..."; BSTR bstr; int iWidthAreaClientHTMLCtrl = 0; // Tractament del SafeArray. HRESULT hrTractarAmbSafeArray = S_OK; SAFEARRAY *pSafeArray = NULL; VARIANT *pElement = NULL; extern CTCCApp TCCApp; //------------------------------------------------ // Obtenir accés al control HTML via ATL. //------------------------------------------------ CComQIPtr spHTMLDocument = this->m_HTMLCtrl.GetHtmlDocument(); if(spHTMLDocument) { CComQIPtr spHTML; spHTMLDocument->get_body(&spHTML); if (spHTML) { bstr = csCodiHTML.AllocSysString(); spHTML->put_innerHTML(bstr); spHTML.Release(); SysFreeString(bstr); csCodiHTML.ReleaseBuffer(); csCodiHTML.FreeExtra(); } else { pSafeArray = ::SafeArrayCreateVector(VT_VARIANT, 0, 1); if (pSafeArray) { hrTractarAmbSafeArray = ::SafeArrayAccessData(pSafeArray,(LPVOID*) &pElement); pElement->vt = VT_BSTR; // HERE! pElement->bstrVal = csCodiHTML.AllocSysString(); hrTractarAmbSafeArray = ::SafeArrayUnaccessData(pSafeArray); hrTractarAmbSafeArray = spHTMLDocument->write(pSafeArray); ::SafeArrayDestroy(pSafeArray); csCodiHTML.ReleaseBuffer(); // HERE TOO! csCodiHTML.FreeExtra(); } } } if (spHTMLDocument) spHTMLDocument.Release(); ------------------------------------------------------------- ------------------------------------------------------------- Where you can see HERE! and HERE TOO! is where that program Rational purify is detecting some memory leaks... do you think that the code is leaking memory? and if so... why? I thought that if I have not called new I should'nt call delete and that whenever the function goes out of scope the local variables are destructed automatically... isn't it true? Thank you in advance...
-
You are missing a call to SafeArrayUnaccessData. Tim Smith I'm going to patent thought. I have yet to see any prior art.
"Resource acquisition is initialization" I love it. ;)
It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)
-
You are missing a call to SafeArrayUnaccessData. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
"Resource acquisition is initialization" I love it. ;)
It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)
-
Hello, I'm debugging my last program... and today I've acquired Rational Purify (trial version) in order to receive help on this task... after running my program under Rational Purify, I've found some memory leaks... but now I'm not sure of which is my name (almost)... let's see, it says that this code is leaking memory: ------------------------------------------------------------- ------------------------------------------------------------- // Variables que permeten la construcció del codi HTML. CString csCodiHTML = "HTML STRING..."; BSTR bstr; int iWidthAreaClientHTMLCtrl = 0; // Tractament del SafeArray. HRESULT hrTractarAmbSafeArray = S_OK; SAFEARRAY *pSafeArray = NULL; VARIANT *pElement = NULL; extern CTCCApp TCCApp; //------------------------------------------------ // Obtenir accés al control HTML via ATL. //------------------------------------------------ CComQIPtr spHTMLDocument = this->m_HTMLCtrl.GetHtmlDocument(); if(spHTMLDocument) { CComQIPtr spHTML; spHTMLDocument->get_body(&spHTML); if (spHTML) { bstr = csCodiHTML.AllocSysString(); spHTML->put_innerHTML(bstr); spHTML.Release(); SysFreeString(bstr); csCodiHTML.ReleaseBuffer(); csCodiHTML.FreeExtra(); } else { pSafeArray = ::SafeArrayCreateVector(VT_VARIANT, 0, 1); if (pSafeArray) { hrTractarAmbSafeArray = ::SafeArrayAccessData(pSafeArray,(LPVOID*) &pElement); pElement->vt = VT_BSTR; // HERE! pElement->bstrVal = csCodiHTML.AllocSysString(); hrTractarAmbSafeArray = ::SafeArrayUnaccessData(pSafeArray); hrTractarAmbSafeArray = spHTMLDocument->write(pSafeArray); ::SafeArrayDestroy(pSafeArray); csCodiHTML.ReleaseBuffer(); // HERE TOO! csCodiHTML.FreeExtra(); } } } if (spHTMLDocument) spHTMLDocument.Release(); ------------------------------------------------------------- ------------------------------------------------------------- Where you can see HERE! and HERE TOO! is where that program Rational purify is detecting some memory leaks... do you think that the code is leaking memory? and if so... why? I thought that if I have not called new I should'nt call delete and that whenever the function goes out of scope the local variables are destructed automatically... isn't it true? Thank you in advance...
I don't think there are leaks in your code. It could be that the complaint is about the line below //HERE, as your tool has no way of knowing that SafeArrayDestroy() will delete the BSTRs in the array. Also, in my opinion, your calls to ReleaseBuffer() and FreeExtra() are not necessary. Cheers, Rob.