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. Errors Using CMap Collections

Errors Using CMap Collections

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelp
7 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.
  • N Offline
    N Offline
    nadiric
    wrote on last edited by
    #1

    I'm somewhat new to the CMap Collection class, and I'm having trouble with my program that's using them. Using: - MSVS2005 - CMapStringToString - CMap I've done an inithash with 1000 as the input. Items seem to be added fine until I get around 100 items... then: Unhandled exception at 0x00435971 in test.exe: 0xC0000005: Access violation reading location 0x726e6e75. This occurs in the CMapStringToString::GetAssocAt function at the point indicated by the arrow: for (pAssoc = m_pHashTable[nHashBucket]; pAssoc != NULL; pAssoc = pAssoc->pNext) { --> if (pAssoc->nHashValue == nHashValue && pAssoc->key == key) return pAssoc; } Based on what MSVS2005 is telling me in Debug, pAssoc is not null, but the nHashValue and key properties "cannot be evaluated". Not sure where to go from here... Thanks for any help.

    A 1 Reply Last reply
    0
    • N nadiric

      I'm somewhat new to the CMap Collection class, and I'm having trouble with my program that's using them. Using: - MSVS2005 - CMapStringToString - CMap I've done an inithash with 1000 as the input. Items seem to be added fine until I get around 100 items... then: Unhandled exception at 0x00435971 in test.exe: 0xC0000005: Access violation reading location 0x726e6e75. This occurs in the CMapStringToString::GetAssocAt function at the point indicated by the arrow: for (pAssoc = m_pHashTable[nHashBucket]; pAssoc != NULL; pAssoc = pAssoc->pNext) { --> if (pAssoc->nHashValue == nHashValue && pAssoc->key == key) return pAssoc; } Based on what MSVS2005 is telling me in Debug, pAssoc is not null, but the nHashValue and key properties "cannot be evaluated". Not sure where to go from here... Thanks for any help.

      A Offline
      A Offline
      ahmad_ali
      wrote on last edited by
      #2

      You probably cause data corruption somewhere else in your code. How do you allocate the LPCTSTR that you insert?

      N 1 Reply Last reply
      0
      • A ahmad_ali

        You probably cause data corruption somewhere else in your code. How do you allocate the LPCTSTR that you insert?

        N Offline
        N Offline
        nadiric
        wrote on last edited by
        #3

        Would that cause this error? If so, that gives me a good place to start debugging. currently I am using the TConvert class _towchar to generate the LPCTSTR's. Within my loop, I checked every iteration to ensure the string was good before trying to insert it... I can try inserting hardcoded strings to eliminate this as a possible cause though... Thanks

        Z 1 Reply Last reply
        0
        • N nadiric

          Would that cause this error? If so, that gives me a good place to start debugging. currently I am using the TConvert class _towchar to generate the LPCTSTR's. Within my loop, I checked every iteration to ensure the string was good before trying to insert it... I can try inserting hardcoded strings to eliminate this as a possible cause though... Thanks

          Z Offline
          Z Offline
          Zac Howland
          wrote on last edited by
          #4

          If the memory for those strings is on the stack, when the scope that is adding them to the map is complete (e.g. the function finishes) all that memory is now invalid. You may still have "valid" data for a while, but all that stack memory is no unallocated/unprotected and will be overwritten eventually. That will cause problems later, if it isn't already.

          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

          N 1 Reply Last reply
          0
          • Z Zac Howland

            If the memory for those strings is on the stack, when the scope that is adding them to the map is complete (e.g. the function finishes) all that memory is now invalid. You may still have "valid" data for a while, but all that stack memory is no unallocated/unprotected and will be overwritten eventually. That will cause problems later, if it isn't already.

            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

            N Offline
            N Offline
            nadiric
            wrote on last edited by
            #5

            So, are you saying that the map does not make copies of the string data, but simply points to the strings I'm sending in? If so, that's useless to me, any way around this?

            Z 1 Reply Last reply
            0
            • N nadiric

              So, are you saying that the map does not make copies of the string data, but simply points to the strings I'm sending in? If so, that's useless to me, any way around this?

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #6

              LPCTSTR is not a string, it is either a const char* or a const wchar_t* depending on whether UNICODE is defined or not. So, yes, the CMap class copies the pointer, but not the string (because it doesn't know you are passing it a string). To get around that, declare your map type as CMap<CString, CString&, int, int>

              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              N 1 Reply Last reply
              0
              • Z Zac Howland

                LPCTSTR is not a string, it is either a const char* or a const wchar_t* depending on whether UNICODE is defined or not. So, yes, the CMap class copies the pointer, but not the string (because it doesn't know you are passing it a string). To get around that, declare your map type as CMap<CString, CString&, int, int>

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                N Offline
                N Offline
                nadiric
                wrote on last edited by
                #7

                Bingo! Thanks, pardon my earlier cynicism... now to post a new problem with the debug heap...

                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