Errors Using CMap Collections
-
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.
-
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.
-
You probably cause data corruption somewhere else in your code. How do you allocate the LPCTSTR that you insert?
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
-
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
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
-
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
-
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?
LPCTSTR
is not a string, it is either aconst char*
or aconst 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 asCMap<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
-
LPCTSTR
is not a string, it is either aconst char*
or aconst 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 asCMap<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