Link to home
Start Free TrialLog in
Avatar of harish_dixit1
harish_dixit1Flag for India

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(&varValue);

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.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of harish_dixit1

ASKER

Sorry these two questions are same but first i submitted to MS Dev zone and i didnt get any response so i thought zone is not correct thats why i posted it in other zone also.
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=(const 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_OUTOFMEMORY);
        }
    }

    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 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).
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