VC++ 6.0中CArray中保存大量对象时内存无法释放的问题,请各位指点
复现条件:
1、编译环境:VC++ 6.0
2、以release方式编译,全部使用默认的编译选项和参数,并且不使用调试器执行该应用程序
3、在数组中存放超过100万个对象
执行程序后,在我的电脑上,接近有85M的内存无法自动释放。
有哪位能够帮忙解释并解决这个问题吗?
btw:受限于历史项目代码,我的应用程序开发环境无法迁移到更新的VisualStudio开发环境,因此不要建议我升级。谢谢。
#include "afxtempl.h"
long int g_cCount = 0; // 记录构造函数调用次数
long int g_dCount = 0; // 记录析构函数调用次数
class CMyTest
{
public:
CString m_str;
CMyTest()
{
m_str = "test string";
g_cCount++;
}
~CMyTest()
{
g_dCount++;
}
};
typedef CArray<CMyTest,CMyTest&> CArrayTest;
void Test()
{
g_cCount = 0;
g_dCount = 0;
CArrayTest testArray;
int count = 1000000;
// 一百万个对象
for(int i =0;i < count; i++ )
{
CMyTest testObj;
testObj.m_str = "test string";
testArray.Add(testObj);
}
testArray.RemoveAll();
testArray.FreeExtra();
}
void CTestMemDlg::OnOK()
{
Test();
CString strInfo;
strInfo.Format("Object construction: %d destruction: %d \r\n",g_cCount,g_dCount);
AfxMessageBox("finish\r\n"+strInfo);
}