harish_dixit1
asked on
Hi, I seen some memory leak in my code during analyzing using DevPartner tool. I tried to fix it but didnt get any success. Please help me here. Description: I am having an array of ATL::CString cla
Hi,
I seen some memory leak in my code during analyzing using DevPartner tool. I tried to fix it but didnt get any success. Please help me here.
Description: I am having an array of ATL::CString class. In loop i am converting these strings to _variant_t type like:
_variant_t varValue = LPCTSTR(m_StrArray[i]);
Then i am having one COM API: AddKey(VARIANT *pVarVal); Which takes VARIANT* as an input parameter.
I am calling this API by passing varValue as a paremeter in the following way:
spSomeInterface->AddKey(&v arValue);
This API internally assign its parameter to _variant_t variable like:
HRESULT AddKey(VARIANT *pVarVal)
{
m_vardata.Attach(*pVarVal) ;
}
When i run this code then DevPartner tool shows memory leak in the following line:
_variant_t varValue = LPCTSTR(m_StrArray[i]);
Please assist me to fix this leak. Its very important to fix because this leak exist in my core component.
I seen some memory leak in my code during analyzing using DevPartner tool. I tried to fix it but didnt get any success. Please help me here.
Description: I am having an array of ATL::CString class. In loop i am converting these strings to _variant_t type like:
_variant_t varValue = LPCTSTR(m_StrArray[i]);
Then i am having one COM API: AddKey(VARIANT *pVarVal); Which takes VARIANT* as an input parameter.
I am calling this API by passing varValue as a paremeter in the following way:
spSomeInterface->AddKey(&v
This API internally assign its parameter to _variant_t variable like:
HRESULT AddKey(VARIANT *pVarVal)
{
m_vardata.Attach(*pVarVal)
}
When i run this code then DevPartner tool shows memory leak in the following line:
_variant_t varValue = LPCTSTR(m_StrArray[i]);
Please assist me to fix this leak. Its very important to fix because this leak exist in my core component.
suppose m_StrArray is the array of CString objects.
for(int i = 0; i < m_StrArray.Count(); i++)
{
_variant_t varValue = LPCTSTR(m_StrArray[i]); //HERE IS MEMORY LEAK
m_spSomeInterface->AddKey(&varValue);
}
AddKey function is defined as:
STDMETHODIMP CMyClass::AddKey(VARIANT *pVarVal)
{
TItemMap::iterator it = m_Map.insert(_variant_t(), true).first;
it->first.Attach(*pVarVal);
}
Where m_Map is the map of _variant_t and bool value. Its member variable. So i am first creating a place holder for variant and then i am attaching it with the input VARIANT* parameter.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for reply.
I tried it as you suggested, but problem still exist.
I am giving stack trace shown by DevPartner tool here.
I am getting memory leak in the following function in comutil.h header file:
inline _variant_t& _variant_t::operator=(cons t wchar_t* pSrc)
{
_COM_ASSERT(V_VT(this) != VT_BSTR || pSrc == NULL || V_BSTR(this) != pSrc);
// Clear the VARIANT (This will SysFreeString() any previous occupant)
//
Clear();
V_VT(this) = VT_BSTR;
if (pSrc == NULL) {
V_BSTR(this) = NULL;
}
else {
V_BSTR(this) = ::SysAllocString(pSrc);
if (V_BSTR(this) == NULL) {
_com_issue_error(E_OUTOFME MORY);
}
}
return *this;
}
And Tool shows the leak in my code:
for(int i = 0; i < m_StrArray.Count(); i++)
{
_variant_t varValue = LPCTSTR(m_StrArray[i]); //HERE IS MEMORY LEAK
m_spSomeInterface->AddKey( &varValue) ;
}
If u want some more info then i can provide it.
Thanks.
I tried it as you suggested, but problem still exist.
I am giving stack trace shown by DevPartner tool here.
I am getting memory leak in the following function in comutil.h header file:
inline _variant_t& _variant_t::operator=(cons
{
_COM_ASSERT(V_VT(this) != VT_BSTR || pSrc == NULL || V_BSTR(this) != pSrc);
// Clear the VARIANT (This will SysFreeString() any previous occupant)
//
Clear();
V_VT(this) = VT_BSTR;
if (pSrc == NULL) {
V_BSTR(this) = NULL;
}
else {
V_BSTR(this) = ::SysAllocString(pSrc);
if (V_BSTR(this) == NULL) {
_com_issue_error(E_OUTOFME
}
}
return *this;
}
And Tool shows the leak in my code:
for(int i = 0; i < m_StrArray.Count(); i++)
{
_variant_t varValue = LPCTSTR(m_StrArray[i]); //HERE IS MEMORY LEAK
m_spSomeInterface->AddKey(
}
If u want some more info then i can provide it.
Thanks.
ASKER
I tried to comment the call of API m_spSomeInterface->AddKey( &varValue) ;
in the loop then DevPartner doesn't show any memory leak. It means the problem is not in the statement where we are converting String to variant. But i think problem is due to API call m_spSomeInterface->AddKey( &varValue) ;. Because this API is doing following assignment internally:
it->first.Attach(*pVarVal) ;
So its attaching param VARIANT* to _variant_t (it->first is of type _variant_t).
in the loop then DevPartner doesn't show any memory leak. It means the problem is not in the statement where we are converting String to variant. But i think problem is due to API call m_spSomeInterface->AddKey(
it->first.Attach(*pVarVal)
So its attaching param VARIANT* to _variant_t (it->first is of type _variant_t).
ASKER
I have attached 2 files. One header file where map data structure is defined and source file where i am accessing the interface method.
I am using smart pointer for interface so no need to call Release on interface pointer.
Source.txt
Header.txt
I am using smart pointer for interface so no need to call Release on interface pointer.
Source.txt
Header.txt
ASKER