Seg Error on C ++ Map Access

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++){ //segfault //(some code goes here) } 

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 ++ )

+4
source share
2 answers

If you are accessing a data structure from different streams, you should have some kind of synchronization. You have to make sure that your object is not accessible simultaneously from different threads. In addition, you must ensure that the changes made by one of the threads are fully visible to the other threads.

A mutex (or critical section , if on Windows) should do the trick: the structure should be locked for every access. This provides exceptional access to the data structure and makes the necessary memory barriers.

Welcome to the multithreaded world!

+2
source

Or:

  • You made a mistake somewhere and ruined your memory. Launch the app through valgrind to find out where.

  • You do not use locks to access objects that you share between threads. You absolutely must do this.


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.

This logic is erroneous.

Segmentation failures are not a definite, reliable indicator of error. This is just one a symptom of a completely unpredictable system that occurs when you call Undefined Behavior.

this code worked fine so far

The same goes for here. Perhaps he worked silently for many years, quietly overwriting bytes in memory to have or not have secure access.


This problem occurs only if the card is empty.

You are just lucky that when the card is empty, your mistake is obvious. Pure chance.

+2
source

All Articles