• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

C++ vector question for you...

Messages
158
Country
us-northcarolina
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:

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);
}
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:

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
 
If you are getting an assertion error on dereferencing an iterator, is it because the size is zero, and you are basically trying to dereference the end() of the vector.

Try checking the size before dereferencing.

Or check begin() == end()
 
Good thought. Since it's not my code -- and there are hardly any comments -- I'm not sure exactly what it's trying to do or how. But I'll bet you're right.

How about if I check 'it' >= begin() and < end() before using it? And begin() == end() and exit if so?

Sounds good I will try it.
 
The problem you have there is nav_db.navaids[row][col].navaids.end() does not point to a valid position within the vector, so it can't be dereferenced.

Now, there is no need to use an iterator with vectors.As they occupy contiguous memory locations for their elements, you can access any of them just by using standard indexes like in typical arrays. Easier to manage and understand IMHO, however not applicable to other type of containers such as lists, which indeed need an iterator to "iterate" them.

So, in your example, instead of:

Code:
for(vector<int>::iterator it = nav_db.navaids[row][col].navaids.begin();
     it!=nav_db.navaids[row][col].navaids.end(); it++)

You could use:

Code:
int iLikeIterator;
for (ind = 0; ind != nav_db.navaids[row][col].navaids.size(); ind++)
{
   int curr_local_ind;
   iLikeIterator = nav_db.navaids[row][col].navaids[ind];
   if (nv_info->type == navaid_type(iLikeIterator,(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;
      }
   }
etc...

Tom
 
Last edited:
The use of (DWORD) for lat/lon is going to cause problems. DWORD doesn't support negative values.
 
Hi DutchI am not a Programingexpert but - - - - - - - - -
inline BOOL is_in_navaid(double Lat,double Lon,naviad_info *nv_info) {
^- - - - - - - may be there is an Er ori n the first Line and perhaps this have to read navaid_info *nv_inofo
Sorry for my English
Wolfgang
 
Tom -- believe me if it was my code I'd have avoided vectors and iterators totally. Except in extreme cases I consider them more trouble than they are worth. But I'm stuck with what they did. Your advice seems solid and I'm doing what you suggest today. I'll report back. I tried Ron's ideas about testing begin() and end() but no joy on that front.

It's important to remember that this code actually works. Somehow, in spite of the debug assertions. The gauge loads and finds navaids in the database and uses them accurately. The problem for me is that the debug assertions are stopping me from using the VS debugger on the rest of the code. I want to suppress them. If I could do that with a statement somewhere saying ITERATOR_DEBUG_LEVEL = 0 I would. But it rejects that with a compile error "duplicate definition".

WarpD -- YES! Exactly. I found a case in another section where the use of DWORD for lat and long was totally trashing the calculation of DTK. Fixed that. I'll look for more of those. In this particular case, they seem to be accounting for the case with an if statement because they are coming up with a row and col. Exactly what the scheme here is I'm not sure, apparently the idea is to group navaids by region based on lat/long so it's quicker to find nearest.

Wolfgang: I'm hesitant to change anything in this code except to avoid the debug assertion. Every time I've done that something breaks. The code actually works, somehow, like I said above. Probably, defensive code somewhere deeper in compensates for these problems in the loading. It's been worked over by several people after the original author and is quite fragile in some areas.

Thanks all! This is a huge help.

Dutch

UPDATE: Tom nailed it. Now I understand. Somehow I thought 'ind = *it' was trying to get an index. But it's going after the content of the location it is pointing too. I was close in the original fix but was using std::distance() to get an index. Just because it has fancy words like vector and iterator doesn't mean it's not just basically a pointer ;)
 
Last edited:
Okay, we can close this case. Tom was right on the money in getting this code to work as written, but the irony is that breaks the gauge. The gauge works when the code fails, and fails when it succeeds! Isn't that lovely? The rest of the code is written to depend on the data being where it is when these attempts at dereferencing the iterator fail! Put them in the right place and the other functions can't find them!

My choices are 1) fix everything, or 2) carry on.

I'm glad to report that there's a way to carry on. Just add the preprocessor directive _INTERATOR_DEBUG_LEVEL=0 to the project properties. The debug assertions are gone and the code continues to fail exactly as it needs to in order to produce the desired result :)

I've been programming for over 40 years, but this is a first. Failure-dependent code. :eek:

Dutch
 
I've been programming for over 40 years, but this is a first. Failure-dependent code. :eek:
Oh my! That is a new one for me as well. At least there's a work-around for the lunacy so you can soldier on... :rotfl:
 
Ha, Bill, I have to laugh when I think about it. Live long enough and you've seen everything. If I had time and resources, or this wasn't a freebie project, I'd fix all that.

OR start over from scratch....

As it is; carry on, carry on...muddle through ya know ...
 
A new one? :D

A lot of things are made to be dependent on a thing or another to fail in a certain way... half of Unix OS was that at one point or another :P

But yeah, I don't think it should happen.
 
Where I come from that's called "defensive programming". I've seen some people who were addicted to it. All of them had poor debugging skills. Instead of figuring out what was wrong, they'd just understand the incorrect outputs and write other functions to fix them. It's very wasteful in every way and it leads to terribly fragile code. But yeah, probably, half the world at some point is dependent on stuff like this hidden away in corporate and government computer systems!

Now that's a pleasant thought to go to sleep with... :scratchch
 
Back
Top