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
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trying to run powershell  in a batch file. How do I do this? 8 117
Trouble with References... 5 63
Problem to open Excel file 15 223
Way to decrease size of apk file 9 109
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…

749 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