Just ran into my first "Program Received signal SIGSEGV" error. Some thoughts ...

So, I am just minding my own buisness and trying to get some code to work when suddenly the debugger tells me that "Program Received signal SIGSEGV".
Apparently that error occurs when the code tries to access a memory location that it can't or shouldn't access.

Interesting ...

So, I started "single-stepping" through the programm and narrowed the error down to the following funtion.
It gets called two times, the first time it is called (with a parameter of 0), everything is fine, but the second time it gets called (with a paramter of 0), the error occurs.

There is a 2D vector (EventTable) to which this function adds a new column in a specified row.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool CreateNewEventEntry(int RowToAddTo )
 {
  //Function to add a cell at the end of the specifed row to the table
  UI.DisplayMessageBox("FUNCTION","CreateNewEventEntry");
  EventTable[RowToAddTo].push_back( {} ) ;
  // add a column (push back a default constructed cell)
  SimulationInformation.NumberOfEventsInString[RowToAddTo] = SimulationInformation.NumberOfEventsInString[RowToAddTo] + 1;

  //Set the member variables of the created entry to the default values, the following line is just an example
  EventTable[RowToAddTo]SimulationInformation.NumberOfEventsInString[RowToAddTo]].ExampleVariable = 0;
		
		
  return true;
	}


So, I had a thought.
Should I create a function for reading from / writing into a vector/array that handles "out of bounds-checking" and all of that?
Beause if I added those things everytime the vector / array gets accessed would result in some even harder to read code ...

Any thoughts on this?
Line 5: EventTable[RowToAddTo] could be EventTable.at(RowToAddTo). The latter always throws exception, if RowToAddTo is out of bounds.
See http://www.cplusplus.com/reference/vector/vector/at/

Line 7: You surely know the shorthand:
++SimulationInformation.NumberOfEventsInString[RowToAddTo];


//Set the member variables of the created entry to the default values, the following line is just an example

Didn't the .push_back( {} ) do that already?
You constructors are bad, if you still have to set the default values.
Line 5: EventTable[RowToAddTo] could be EventTable.at(RowToAddTo). The latter always throws exception, if RowToAddTo is out of bounds.
See http://www.cplusplus.com/reference/vector/vector/at/


I will have a look at ussing the "vector at" thing then ...


Didn't the .push_back( {} ) do that already?
You constructors are bad, if you still have to set the default values.


Constructors?
I never defined / used any ...
But I looked it up and yes, it basically can be used to set initial values and stuff like that.
Something else that I will use from now on ...
Like I said, what is weirt is that the error occurs like this:

1
2
3
4
5
6
7
8
//... some code
//Current number of elements in EventTable: 0

CreateNewEventEntry(0);  //everything is fine
//Current number of elements in EventTable: 1
CreateNewEventEntry(0);  //Error occurs

//... some code 


It's not like it lots of elements get added to EventTable, just 2.
So why would adding 2 elements to a 2d vector cause this problem?

I would not question it if adding LOTS OF ELEMENTS (like 100000000000000000000000000000) wouldd cause issues, but just adding 2 SHOULD be fine, right?
Last edited on
SimulationInformation.NumberOfEventsInString[RowToAddTo] seems redundant.
Doesn't it just mimic the value of EventTable[RowToAddTo].size()?
SimulationInformation.NumberOfEventsInString[RowToAddTo] seems redundant.
Doesn't it just mimic the value of EventTable[RowToAddTo].size()?


Actually, yes it does!
When I first implemented this, I wasn't quite sure that "size" would really return the "number of elements" or the actual "size" (as in how many bytes of memory) the vector uses.

I am currently rewriting this project completley ...

And using an actual constructor seems to have solved my SIGSEGV and "terminate called reccursively" error!


I am not using "vector at" because I can't figure out how that would work with 2D vectors ...
Last edited on
So why would adding 2 elements to a 2d vector cause this problem?
You're making the classic mistake of thinking "that can't happen" and becoming paralyzed as a result. Don't speculate. Investigate. Change [x] to .at(x) as keskiverto suggested and find out for certain. Maybe the code isn't really adding 2 elements. Maybe you're deleting an element. Maybe you're using an uninitialized variable and the index in the second call is 2389478433. Maybe you're corrupting the heap somewhere else and turning the vector into garbage. There are million reasons why it might be wrong.

I am not using "vector at" because I can't figure out how that would work with 2D vectors

Lets say that you have vector<vector<Event>> table;
The table is a vector. Each element of table is a vector<Event>. A vector.

An element of table: table[row] is same as table.at(row) ... is a vector.

You access element of vector with [] or .at()
A "cell", an element of one of the "rows": table[row][col] is same as table.at(row).at(col) is same as table[row].at(col) is same as table.at(row)[col]
I am not using "vector at" because I can't figure out how that would work with 2D vectors

Lets say that you have vector<vector<Event>> table;
The table is a vector. Each element of table is a vector<Event>. A vector.

An element of table: table[row] is same as table.at(row) ... is a vector.

You access element of vector with [] or .at()
A "cell", an element of one of the "rows": table[row][col] is same as table.at(row).at(col) is same as table[row].at(col) is same as table.at(row)[col]


That makes sense!
And for "N dimensions", just keep adding .at() ...
Wow, doing "more then one-dimensional stuff" isn't actually that hard ...


So why would adding 2 elements to a 2d vector cause this problem?
You're making the classic mistake of thinking "that can't happen" and becoming paralyzed as a result. Don't speculate. Investigate. Change [x] to .at(x) as keskiverto suggested and find out for certain. Maybe the code isn't really adding 2 elements. Maybe you're deleting an element. Maybe you're using an uninitialized variable and the index in the second call is 2389478433. Maybe you're corrupting the heap somewhere else and turning the vector into garbage. There are million reasons why it might be wrong.


I am currently investigating ...
And finding more "bugs" ...

For example: Despite the fact that my constructor sets all member variables of a newly created element in that 2dVector to 0, they have seemingly random values (very high values!)
they have seemingly random values

consider yourself lucky. If it would have 0 randomly, then you would not even suspect that part of the code.
they have seemingly random values

consider yourself lucky. If it would have 0 randomly, then you would not even suspect that part of the code.


What could be the cause of this?
Something that is located "before" these values in the memory that "bleeds over" (overflows into the next few bytes in memory) into those memory locations?


Also, it is weird:
Something like this works:
1
2
3
4
5
CreateNewEventEntry(0);
CreateNewEventEntry(0);
CreateNewEventEntry(0);
CreateNewEventEntry(0);
CreateNewEventEntry(0);


No problems, everything gets created with all zeros, just like the constructor says it should be!

But if this function gets called inside of another function that gets called by another function then I get the "garbage" ...
Weird ....
Last edited on
Registered users can post here. Sign in or register to post.