Solved

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

Posted on 2009-07-13
6
476 Views
Last Modified: 2013-12-04
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

0
Comment
Question by:harish_dixit1
  • 4
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 24841626
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
 

Author Comment

by:harish_dixit1
ID: 24841876
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
 

Author Comment

by:harish_dixit1
ID: 24841923
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
 

Author Comment

by:harish_dixit1
ID: 24846820
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
 

Author Comment

by:harish_dixit1
ID: 24856435
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now