Memory leak in VC++ 6.0 when store lots of objects in CArray,please help me!
-
1.dev enviroment:VC++ 6.0 2.build program in 'release' 3.store 1000000 simple objects in CArray 4.run the program without debug After running the code , it's about 85M memory cannot be release on my computer. Can anyone explain and solve the problem for me? Thanks. BTW:Because of the legency project code,my dev IDE is not allowed to update to the latest VisualStudio. So i have to solve the problem in VC 6.0 IDE. #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); }
-
1.dev enviroment:VC++ 6.0 2.build program in 'release' 3.store 1000000 simple objects in CArray 4.run the program without debug After running the code , it's about 85M memory cannot be release on my computer. Can anyone explain and solve the problem for me? Thanks. BTW:Because of the legency project code,my dev IDE is not allowed to update to the latest VisualStudio. So i have to solve the problem in VC 6.0 IDE. #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); }
It's a long time since I did any MFC programming, but I suspect that the problem is the definition of CArrayTest. Try making this
typedef CArray<CMyTest,CMyTest> CArrayTest;
(no reference). This will cause every element to be copied into the array, but should work.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill
-
1.dev enviroment:VC++ 6.0 2.build program in 'release' 3.store 1000000 simple objects in CArray 4.run the program without debug After running the code , it's about 85M memory cannot be release on my computer. Can anyone explain and solve the problem for me? Thanks. BTW:Because of the legency project code,my dev IDE is not allowed to update to the latest VisualStudio. So i have to solve the problem in VC 6.0 IDE. #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); }
-
1.dev enviroment:VC++ 6.0 2.build program in 'release' 3.store 1000000 simple objects in CArray 4.run the program without debug After running the code , it's about 85M memory cannot be release on my computer. Can anyone explain and solve the problem for me? Thanks. BTW:Because of the legency project code,my dev IDE is not allowed to update to the latest VisualStudio. So i have to solve the problem in VC 6.0 IDE. #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); }
lostangels wrote:
CArrayTest testArray; int count = 1000000; for(int i =0;i < count; i++ ) { CMyTest testObj; testObj.m_str = "test string"; testArray.Add(testObj); }
What will happen if you change this code to CArrayTest testArray; int count = 1000000; testArray.SetSize(count, 100); for(int i = 0; i < count; i++ ) { CMyTest testObj; testObj.m_str = "test string"; testArray.SetAt(i, testObj); }
-
1.dev enviroment:VC++ 6.0 2.build program in 'release' 3.store 1000000 simple objects in CArray 4.run the program without debug After running the code , it's about 85M memory cannot be release on my computer. Can anyone explain and solve the problem for me? Thanks. BTW:Because of the legency project code,my dev IDE is not allowed to update to the latest VisualStudio. So i have to solve the problem in VC 6.0 IDE. #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); }
I am not convinced there is a leak ... Please explain this statement
After running the code , it's about 85M memory cannot be release on my computer
How exactly do you know it isn't released? For example looking at the memory use in Task manager means nothing if that is all you are using. So has this statement got some actual tool basis? If all you are doing is running task manager run the Test for me twice please one after another.
void CTestMemDlg::OnOK()
{
Test();
test(); // RUN test a second time .. if you are bleeding memory use will doubleCString strInfo;
strInfo.Format("Object construction: %d destruction: %d \r\n",g_cCount,g_dCount);
AfxMessageBox("finish\r\n"+strInfo);
}If the memory use doesn't double to 170M you aren't leaking memory at all you just misunderstand what windows task manager is reporting.
In vino veritas