Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory leak in VC++ 6.0 when store lots of objects in CArray,please help me!

Memory leak in VC++ 6.0 when store lots of objects in CArray,please help me!

Scheduled Pinned Locked Moved C / C++ / MFC
helpannouncementc++visual-studiodebugging
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lostangels
    wrote on last edited by
    #1

    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); }

    D L V L 4 Replies Last reply
    0
    • L lostangels

      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); }

      D Offline
      D Offline
      Daniel Pfeffer
      wrote on last edited by
      #2

      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 Reply Last reply
      0
      • L lostangels

        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); }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I think you will find that RemoveAll will remove all the elements from the array but will not actually dispose them (i.e. call their destructors). Should be easy enough to test.

        1 Reply Last reply
        0
        • L lostangels

          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); }

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          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 Reply Last reply
          0
          • L lostangels

            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); }

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #5

            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 double

            CString 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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups