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

harish_dixit1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
As in http://www.experts-exchange.com/Programming/Languages/CPP/Q_24565906.html

You are probably adding a CString (at least 'varValue = LPCTSTR(m_StrArray[i]);' suggests that), which means that first a new variant is constructed and assigned, which now is kinda 'orphaned'. Try

HRESULT AddKey(VARIANT *pVarVal)
{
  m_vardata.Clear(); // <---- !
  m_vardata.Attach(*pVarVal);
}

instead.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
harish_dixit1Author Commented:
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.
0
harish_dixit1Author Commented:
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.
0
harish_dixit1Author Commented:
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).
0
harish_dixit1Author Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.