crash related to CString acces violating
-
I have this : struct struct_X { struct_X() : nX(0) { } int nX; CString m_strName; }; struct_X *ptrX1=NULL; struct_X *ptrX2=NULL; i have a loop like this : for(int Idx = 0; Condition; Idx++) { ... ... if(nCount==0) ptrX1= new struct_X; else ptrX1=ReallocArray((nCount+1), nCount, ptrX2); // copy the content of ptrX2 to ptrX1 and delete ptrX2 (**) if(ptrTmp==NULL) break; ptrX2=ptrX1; ptrX2[nCount].nX = 1; ptrX2[nCount].m_strName = strTempName; // Here I have an a crash related to CString acces violating nCount++; } in the first loop ( nCount = 0) there is no problem but after the ReallocArray (**) i get the problem in ptrX2[nCount].m_strName Have any one idea how to solve this?
-
I have this : struct struct_X { struct_X() : nX(0) { } int nX; CString m_strName; }; struct_X *ptrX1=NULL; struct_X *ptrX2=NULL; i have a loop like this : for(int Idx = 0; Condition; Idx++) { ... ... if(nCount==0) ptrX1= new struct_X; else ptrX1=ReallocArray((nCount+1), nCount, ptrX2); // copy the content of ptrX2 to ptrX1 and delete ptrX2 (**) if(ptrTmp==NULL) break; ptrX2=ptrX1; ptrX2[nCount].nX = 1; ptrX2[nCount].m_strName = strTempName; // Here I have an a crash related to CString acces violating nCount++; } in the first loop ( nCount = 0) there is no problem but after the ReallocArray (**) i get the problem in ptrX2[nCount].m_strName Have any one idea how to solve this?
Hi, Looks like a problem with your
ReallocArray()
.ATL::CString
and MFCCString
are simplytypedef ATL::CStringT<TCHAR> CString;
so at first your code may be crashing because of a wrongTCHAR
type. Anyhow you should always use theCString
copy operator and not try to manipulate internal data structures as you do. cheers, ARWhen the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
-
Hi, Looks like a problem with your
ReallocArray()
.ATL::CString
and MFCCString
are simplytypedef ATL::CStringT<TCHAR> CString;
so at first your code may be crashing because of a wrongTCHAR
type. Anyhow you should always use theCString
copy operator and not try to manipulate internal data structures as you do. cheers, ARWhen the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
ReallocArray() just get the ptrX2 and copy its elements to the new array ptrX1 which has number of ptrX2's elements + 1 and then delete ptrX2. So I think the problem is no related to this. I agree that there is problem somewhere in CString opreation but where :omg:
-
ReallocArray() just get the ptrX2 and copy its elements to the new array ptrX1 which has number of ptrX2's elements + 1 and then delete ptrX2. So I think the problem is no related to this. I agree that there is problem somewhere in CString opreation but where :omg:
khaliloenit wrote:
ReallocArray() just get the ptrX2 and copy its elements to the new array ptrX1 which has number of ptrX2's elements + 1 and then delete ptrX2.
And when do you construct your new
struct_X
? Drop your dangerous and painful homecooked memory management forATL::CSimpleArray<struct_X>
orstd::vector<struct_X>
and your problem will disappear :) cheers, ARWhen the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
-
I have this : struct struct_X { struct_X() : nX(0) { } int nX; CString m_strName; }; struct_X *ptrX1=NULL; struct_X *ptrX2=NULL; i have a loop like this : for(int Idx = 0; Condition; Idx++) { ... ... if(nCount==0) ptrX1= new struct_X; else ptrX1=ReallocArray((nCount+1), nCount, ptrX2); // copy the content of ptrX2 to ptrX1 and delete ptrX2 (**) if(ptrTmp==NULL) break; ptrX2=ptrX1; ptrX2[nCount].nX = 1; ptrX2[nCount].m_strName = strTempName; // Here I have an a crash related to CString acces violating nCount++; } in the first loop ( nCount = 0) there is no problem but after the ReallocArray (**) i get the problem in ptrX2[nCount].m_strName Have any one idea how to solve this?
1.) c!=c++ 2.) c++ has a destructor and constructor that initializes the vtable too. 3.) c++ has not a realloc it knows only new and delete (its a immanent failure of c++) 4.) if you realloc an array of classes and they have virtual functions (ie. virtual void doanything(); ) you MUST call the constructor by yourself (and the destructor too). 5.) how to do: for(i=last_valid_element+1;istruct_X::struct_X(); 5a) remember: struct is in c++ the same as class. 6.) destructor for all elements or delete [] ptrX2 ( calls implicit (ptrX2+i)->struct_X::~struct_X(); ) so far and nice day.