Microsoft KB Archive/127194

= FIX: CMap Template Class Leaks Non-Primitive KEY Objects =

Article ID: 127194

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft Foundation Class Library 4.2, when used with:
 * Microsoft Visual C++ 2.0 Professional Edition

-



This article was previously published under Q127194



SYMPTOMS
When using the CMap template class, users may notice a memory leak when using non-primitive objects as KEY values (for example, CString).



CAUSE
The CMap class does not call the destructor for its KEYs when it does a RemoveAll.



RESOLUTION
Because the CMap class is implemented in AFXTEMPL.H, users can add a single line to the CMap::RemoveAll member function in this file to correct this problem. The new line is marked with a //NOTE: in the following code: template void CMap::RemoveAll {   ASSERT_VALID(this);

if (m_pHashTable != NULL) {       // destroy elements (values and keys) for (UINT nHash = 0; nHash < m_nHashTableSize; nHash++) {           CAssoc* pAssoc; for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL;             pAssoc = pAssoc->pNext) {               DestructElements(&pAssoc->value, 1);

//NOTE: Additional call to destruct the keys! DestructElements(&pAssoc->key, 1); }       }    }

// free hash table delete[] m_pHashTable; m_pHashTable = NULL;

m_nCount = 0; m_pFreeList = NULL; m_pBlocks->FreeDataChain; m_pBlocks = NULL; }



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was corrected in the Microsoft Foundation Classes version 3.1 included with Microsoft Visual C++, 32-bit Edition, version 2.1.

Additional query words: CString CMap 3.00 KEY 2.00

Keywords: kbbug kbcode kbcollectionclass kbfix kbnoupdate kbtemplate kbvc200fix KB127194

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.