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. "Small" bug in CSimpleArray ???

"Small" bug in CSimpleArray ???

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancehelptutorialquestion
8 Posts 2 Posters 2 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.
  • E Offline
    E Offline
    Ernesto D
    wrote on last edited by
    #1

    Hi all, ibe been trying to find a memory leak for the last 5 hours, and then i discovered what i think is a "small bug" in CSimpleArray<> collection helper, here´s an example: class acls { public: CString m_str; // constructor, copy constructor, etc. here }; // in some function... // create some objects to add... acls mycls; mycls.m_str="ABC"; acls another; another.m_str="CDE"; // and create the array & add the objects CSimpleArray arr; arr.Add(mycls); arr.Add(another); arr.RemoveAt(1); this results in a memory leak pointing to the 2nd object added to the array, the interesting part is, that if you remove the very first item instead (RemoveAt(0)) there is no leak. Can NE1 suggest a way arround this that does not involve NOT using CSimpleArray? my project is allmost finished, and rewritting everything not to use these arrays would take me forever. thanks!

    N E 2 Replies Last reply
    0
    • E Ernesto D

      Hi all, ibe been trying to find a memory leak for the last 5 hours, and then i discovered what i think is a "small bug" in CSimpleArray<> collection helper, here´s an example: class acls { public: CString m_str; // constructor, copy constructor, etc. here }; // in some function... // create some objects to add... acls mycls; mycls.m_str="ABC"; acls another; another.m_str="CDE"; // and create the array & add the objects CSimpleArray arr; arr.Add(mycls); arr.Add(another); arr.RemoveAt(1); this results in a memory leak pointing to the 2nd object added to the array, the interesting part is, that if you remove the very first item instead (RemoveAt(0)) there is no leak. Can NE1 suggest a way arround this that does not involve NOT using CSimpleArray? my project is allmost finished, and rewritting everything not to use these arrays would take me forever. thanks!

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      Ernesto D. wrote: mycls another; Shouldn't this be: acls another; I'd be very, very surprised if there was a problem with CSimpleArray. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

      E 1 Reply Last reply
      0
      • E Ernesto D

        Hi all, ibe been trying to find a memory leak for the last 5 hours, and then i discovered what i think is a "small bug" in CSimpleArray<> collection helper, here´s an example: class acls { public: CString m_str; // constructor, copy constructor, etc. here }; // in some function... // create some objects to add... acls mycls; mycls.m_str="ABC"; acls another; another.m_str="CDE"; // and create the array & add the objects CSimpleArray arr; arr.Add(mycls); arr.Add(another); arr.RemoveAt(1); this results in a memory leak pointing to the 2nd object added to the array, the interesting part is, that if you remove the very first item instead (RemoveAt(0)) there is no leak. Can NE1 suggest a way arround this that does not involve NOT using CSimpleArray? my project is allmost finished, and rewritting everything not to use these arrays would take me forever. thanks!

        E Offline
        E Offline
        Ernesto D
        wrote on last edited by
        #3

        heres another example, this one ibe just created as a normal Win32 console app, and included afx.h and atlbase.h to my stdafx.h. Ibe just compiled & executed this, and got the same leak // leak.cpp : Defines the entry point for the console application. #include "stdafx.h" class CClass { public: CClass(){} CClass(const CClass& rv) { m_member=rv.m_member;} const CClass& operator=(const CClass& rv){m_member=rv.m_member; return *this;} CString m_member; }; int main(int argc, char* argv[]) { CClass c1; CClass c2; c1.m_member="ABC"; c2.m_member="DEF"; CSimpleArray arr; arr.Add(c1); arr.Add(c2); arr.RemoveAt(1); return 0; } the results (in debug window) after program exits are: Detected memory leaks! Dumping objects -> strcore.cpp(118) : {44} normal block at 0x003E2498, 16 bytes long. Data: < DEF > 01 00 00 00 03 00 00 00 03 00 00 00 44 45 46 00 Object dump complete. as a side note, if you remove the item at position 0 of the array instead of 1, then tere is no leak. this IS a bug in CSimpleArray isnt it?

        N 1 Reply Last reply
        0
        • N Neville Franks

          Ernesto D. wrote: mycls another; Shouldn't this be: acls another; I'd be very, very surprised if there was a problem with CSimpleArray. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

          E Offline
          E Offline
          Ernesto D
          wrote on last edited by
          #4

          yep, my mistake there. check out the new "real world" example ibe just posted. I got the same leak.

          N 1 Reply Last reply
          0
          • E Ernesto D

            yep, my mistake there. check out the new "real world" example ibe just posted. I got the same leak.

            N Offline
            N Offline
            Neville Franks
            wrote on last edited by
            #5

            Ernesto D. wrote: check out the new "real world" example ibe just posted. And where would that be? Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

            E 1 Reply Last reply
            0
            • N Neville Franks

              Ernesto D. wrote: check out the new "real world" example ibe just posted. And where would that be? Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

              E Offline
              E Offline
              Ernesto D
              wrote on last edited by
              #6

              Umm, just below this post man! :) i added it as a reply

              1 Reply Last reply
              0
              • E Ernesto D

                heres another example, this one ibe just created as a normal Win32 console app, and included afx.h and atlbase.h to my stdafx.h. Ibe just compiled & executed this, and got the same leak // leak.cpp : Defines the entry point for the console application. #include "stdafx.h" class CClass { public: CClass(){} CClass(const CClass& rv) { m_member=rv.m_member;} const CClass& operator=(const CClass& rv){m_member=rv.m_member; return *this;} CString m_member; }; int main(int argc, char* argv[]) { CClass c1; CClass c2; c1.m_member="ABC"; c2.m_member="DEF"; CSimpleArray arr; arr.Add(c1); arr.Add(c2); arr.RemoveAt(1); return 0; } the results (in debug window) after program exits are: Detected memory leaks! Dumping objects -> strcore.cpp(118) : {44} normal block at 0x003E2498, 16 bytes long. Data: < DEF > 01 00 00 00 03 00 00 00 03 00 00 00 44 45 46 00 Object dump complete. as a side note, if you remove the item at position 0 of the array instead of 1, then tere is no leak. this IS a bug in CSimpleArray isnt it?

                N Offline
                N Offline
                Neville Franks
                wrote on last edited by
                #7

                Test the Add() and RemoveAt() and make sure the all return TRUE. SDK docs indicate CAtlArray should be used instead of CSimpleArray. I'd also add a Destructor to CClass and check when it is being called as that is where the CString will be deleted. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

                E 1 Reply Last reply
                0
                • N Neville Franks

                  Test the Add() and RemoveAt() and make sure the all return TRUE. SDK docs indicate CAtlArray should be used instead of CSimpleArray. I'd also add a Destructor to CClass and check when it is being called as that is where the CString will be deleted. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

                  E Offline
                  E Offline
                  Ernesto D
                  wrote on last edited by
                  #8

                  Thanks, ibe found what the problem is, check out the implementation of the CSimpleArray class template included in VC6: BOOL RemoveAt(int nIndex) { if(nIndex != (m_nSize - 1)) { m_aT[nIndex].~T(); memmove((void*)&m_aT[nIndex], (void*)&m_aT[nIndex + 1], (m_nSize - (nIndex + 1)) * sizeof(T)); } m_nSize--; return TRUE; } as you can notice, if you remove the last element, the element is NOT destroyed. ibe been told however, that this "small glitch" is fixed in .NET, i guess the rest of us "mortals" using VC6 are stuck with it.

                  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