I ran into some weird problem in some code I'm working on. Basically, what happens when I try to get some information from a blank card, the segfaults program. Here's the corresponding code: (note that a struct Pair is a data structure as defined earlier, and sendMasks is a std :: map, which is good)
std::map<std::string*, struct Pair*>::iterator it; for(it = sendMasks->begin(); it != sendMasks->end(); it++){
I know the map pointer is good; I can do
it = sendMasks->begin(); it = sendMasks->end();
before my cycle, and it is not a segfault at all.
Now, if I put the following test before the for loop, it will be segfault:
if( sendMasks->empty() )
Like any other attempt to determine if a card is empty.
This problem occurs only if the card is empty. My only thought in this matter was that since I am updating sendMasks in a separate thread, it is possible that it was not updated correctly; which, however, makes no sense because it will only happen if the card is empty, and this code has worked perfectly so far. Any other thoughts on what might happen?
EDIT: I realized what the problem is.
In the previous part of my code, I created a new char * array and placed this pointer in another array of length 4. Then I put the NULL character at the end of my new array, but accidentally only made an index from the first array that left the end of the array and rewritten the pointer. One way or another, this from time to time was able to work normally. (valgrind does not detect this problem)
The sequence was something like this:
object* = NULL; //(overwritten memory) object->method(); //Inside object::method() : map->size(); //segfault. Gets an offset of 0x24 into the object, //which is NULL to begin with. memory location 0x24 = invalid
I did not expect the instance of the object itself to be empty, because in Java this method call will fail before it does, and in C it will be done in a completely different way (I do not do much object-oriented programming in C ++ )