- Messages
- 158
- Country

This is for you c++ code gurus who like a challenge. I'm baffled. And I've tried everything I can think of and done quite a bit of research. I will admit I don't understand vectors or iterators well at all.
In the KNL990B gauge I'm trying to fix, I can't use the debugger because it crashes with a debug assertion in the early going while it's loading the navdata from disk. The error is "Vector Iterator Cannot be Dereferenced". The offending code is:
I've marked the statement that asserts the debug condition. This pattern is repeated in roughly 14 other spots in the code, and they all crash with the same error at the same point. But I can compile this for release, and the program works! That is, no crash and it navigates fairly well. I'm using VS2013.
The Internet suggests several fixes for this problem. Mainly, replace:
with:
And when I do this, the debug assert goes away but another appears: "Vector Iterator not incrementable" - which happens when the loop returns back to the 'for' statement.
I have gotten around that with another Internet solution which is to replace the 'for' with this:
and commenting any 'ind ='. So the "fixed" version looks like this:
Now, there are no debug assertions and it apparently runs smoothly. I fixed the other 13 cases where this pattern was used in the same way.
But now the gauge is broken. It won't find nearest navaids, and the flight plan can't be created, etc, etc. So I conclude that these fixes, which seem harmless to my feeble understanding of vectors and iterators, change the behavior of the code in bad ways.
All I want is to preserve the functionality of the code while avoiding the debug assertion. I don' care how ugly the solution is so long as it works.
Is there a way to test for the error before it does the 'int ind = *it' and not do it when the iterator can't be dereferenced? and further, is there a way to know, at the bottom of the for loop, whether the iterator is about to cross over into 'unincremental' territory and cause that debug assertion?
Like this:
Dutch
In the KNL990B gauge I'm trying to fix, I can't use the debugger because it crashes with a debug assertion in the early going while it's loading the navdata from disk. The error is "Vector Iterator Cannot be Dereferenced". The offending code is:
Code:
inline BOOL is_in_navaid(double Lat,double Lon,naviad_info *nv_info) {
if( (nv_info->type==NAVAID_WPT) || (nv_info->type==NAVAID_SUP) )
return(FALSE);
int row = Lon<0?(DWORD)(360+Lon):(DWORD)(Lon);
int col = (DWORD)(90+Lat);
for(vector<int>::iterator it = nav_db.navaids[row][col].navaids.begin();
it!=nav_db.navaids[row][col].navaids.end(); it++) {
int ind = *it; // <--- This causes "vector iterator cannot be deferenced"
int curr_local_ind;
if (nv_info->type == navaid_type(ind,(long *)&curr_local_ind)) {
switch(nv_info->type) {
case NAVAID_NDB: {
if(is_ndb_equal(nv_info->local_index,curr_local_ind))
return(TRUE);
}
break;
case NAVAID_VOR: {
if(is_vor_equal(nv_info->local_index,curr_local_ind))
return(TRUE);
}
break;
}
}
return (FALSE);
}
The Internet suggests several fixes for this problem. Mainly, replace:
Code:
int ind = *it;
with:
Code:
int ind = std::distance(nav_db.navaids[row][col].navaids.begin(), it);
And when I do this, the debug assert goes away but another appears: "Vector Iterator not incrementable" - which happens when the loop returns back to the 'for' statement.
I have gotten around that with another Internet solution which is to replace the 'for' with this:
Code:
for (ind = 0; ind != nav_db.navaids[row][col].navaids.size(); ind++) {
and commenting any 'ind ='. So the "fixed" version looks like this:
Code:
inline BOOL is_in_navaid(double Lat,double Lon,naviad_info *nv_info) {
int ind;
if( (nv_info->type==NAVAID_WPT) || (nv_info->type==NAVAID_SUP) )
return(FALSE);
int row = Lon<0?(DWORD)(360+Lon):(DWORD)(Lon);
int col = (DWORD)(90+Lat);
for (ind = 0; ind != nav_db.navaids[row][col].navaids.size(); ind++) {
int curr_local_ind;
if (nv_info->type == navaid_type(ind,(long *)&curr_local_ind)) {
switch(nv_info->type) {
case NAVAID_NDB: {
if(is_ndb_equal(nv_info->local_index,curr_local_ind))
return(TRUE);
}
break;
case NAVAID_VOR: {
if(is_vor_equal(nv_info->local_index,curr_local_ind))
return(TRUE);
}
break;
}
}
return (FALSE);
}
Now, there are no debug assertions and it apparently runs smoothly. I fixed the other 13 cases where this pattern was used in the same way.
But now the gauge is broken. It won't find nearest navaids, and the flight plan can't be created, etc, etc. So I conclude that these fixes, which seem harmless to my feeble understanding of vectors and iterators, change the behavior of the code in bad ways.
All I want is to preserve the functionality of the code while avoiding the debug assertion. I don' care how ugly the solution is so long as it works.
Is there a way to test for the error before it does the 'int ind = *it' and not do it when the iterator can't be dereferenced? and further, is there a way to know, at the bottom of the for loop, whether the iterator is about to cross over into 'unincremental' territory and cause that debug assertion?
Like this:
Code:
inline BOOL is_in_navaid(double Lat,double Lon,naviad_info *nv_info) {
int ind;
if( (nv_info->type==NAVAID_WPT) || (nv_info->type==NAVAID_SUP) )
return(FALSE);
int row = Lon<0?(DWORD)(360+Lon):(DWORD)(Lon);
int col = (DWORD)(90+Lat);
for(vector<int>::iterator it = nav_db.navaids[row][col].navaids.begin();
it!=nav_db.navaids[row][col].navaids.end(); it++) {
if [interator 'it' can be dereferenced] {
ind = *it;
} else {
ind = 0;
}
int curr_local_ind;
if (nv_info->type == navaid_type(ind,(long *)&curr_local_ind)) {
switch(nv_info->type) {
case NAVAID_NDB: {
if(is_ndb_equal(nv_info->local_index,curr_local_ind))
return(TRUE);
}
break;
case NAVAID_VOR: {
if(is_vor_equal(nv_info->local_index,curr_local_ind))
return(TRUE);
}
break;
}
if [iterator 'it' can't be incremented]
break;
}
return (FALSE);
}
Dutch







