"Small" bug in CSimpleArray ???
-
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!
-
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!
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 -
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!
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?
-
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 -
yep, my mistake there. check out the new "real world" example ibe just posted. I got the same leak.
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
-
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
-
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?
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
-
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
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.