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. ATL / WTL / STL
  4. crash related to CString acces violating

crash related to CString acces violating

Scheduled Pinned Locked Moved ATL / WTL / STL
helptutorialquestion
5 Posts 3 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.
  • M Offline
    M Offline
    MrKBA
    wrote on last edited by
    #1

    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?

    A M 2 Replies Last reply
    0
    • M MrKBA

      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?

      A Offline
      A Offline
      Alain Rist
      wrote on last edited by
      #2

      Hi, Looks like a problem with your ReallocArray(). ATL::CString and MFC CString are simply typedef ATL::CStringT<TCHAR> CString; so at first your code may be crashing because of a wrong TCHAR type. Anyhow you should always use the CString copy operator and not try to manipulate internal data structures as you do. cheers, AR

      When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

      M 1 Reply Last reply
      0
      • A Alain Rist

        Hi, Looks like a problem with your ReallocArray(). ATL::CString and MFC CString are simply typedef ATL::CStringT<TCHAR> CString; so at first your code may be crashing because of a wrong TCHAR type. Anyhow you should always use the CString copy operator and not try to manipulate internal data structures as you do. cheers, AR

        When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

        M Offline
        M Offline
        MrKBA
        wrote on last edited by
        #3

        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:

        A 1 Reply Last reply
        0
        • M MrKBA

          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:

          A Offline
          A Offline
          Alain Rist
          wrote on last edited by
          #4

          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 for ATL::CSimpleArray<struct_X> or std::vector<struct_X> and your problem will disappear :) cheers, AR

          When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

          1 Reply Last reply
          0
          • M MrKBA

            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?

            M Offline
            M Offline
            mbue
            wrote on last edited by
            #5

            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.

            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