CMapStringToOb map;
CAge* pa;map.SetAt( "Bart", new CAge( 13 ) );
map.SetAt( "Lisa", new CAge( 11 ) );
map.SetAt( "Homer", new CAge( 36 ) );
map.SetAt( "Marge", new CAge( 35 ) );
ASSERT( map.Lookup( "Lisa", ( CObject*& ) pa ) ); // Is "Lisa" in the map?
map.RemoveAll();以上代码是不是有内存泄漏的问题?

解决方案 »

  1.   

    yes,你要遍历这个map,删除掉每一个元素,
    另外这种方式有问题的,你应该先查询一下里面是否有一个同名元素,如果没有再加进去,否则如果同名的话,内存泄漏是无法避免的
      

  2.   

    void RemoveAll( );ResRemoves all the elements from this map and destroys the CString key objects. The CObject objects referenced by each key are not destroyed. The RemoveAll function can cause memory leaks if you do not ensure that the referenced CObject objects are destroyed. The function works correctly if the map is already empty.
      

  3.   

    map里面存放的只是Object的引用而已,所以释放工作还是由你自己做的。
      

  4.   

    给你一个例子,你可以转换一下,很容易。
    也就是说:如果CObArray中的元素是指针,CObArray的析构函数是不会把指针的指向地址free掉的!所以,如果要free指针的指向地址的话,就需要自己手工free,如下:for (i=0;i < myArray.GetSize();i++)
       {
          CBase* pBase = (CBase*) myArray.ElementAt(i);
          delete pBase ;
       }
    myArray.ReMoveAll();