2

I'm a bit stumped with, what I thought was, some simple code. It was previously working, so I wasn't sure why it stopped working. This is a single threaded program.

    Model *model;
    ModelMap::iterator model_map_iterator;

    cout << this->models.size() << endl;

    for( model_map_iterator = this->models.begin(); model_map_iterator != this->models.end(); model_map_iterator++ ){

        cout << "what" << endl;
        //model = model_map_iterator->second;
        //cout << *model;

    }

    cout << this->models.size() << endl;

And here's the output when there's 1, 2 and 4 models, respectively.

$ program
    1
    what
    1

$ program
    2
    what
    what
    2

$ program
    4
    what
    what
    4

As you can see, for any number of models greater than 2, the number of "what" outputs is 2. I've tried it for 2-5 models. All of them produce two "what" strings, yet output the correct size of the std::map.

Could anyone please help me find out what I'm doing wrong?

Thanks in advance...

UPDATE:

Committed entire codebase for debugging:

https://github.com/homer6/modeler/tree/crudepythonmodeler

commit ( 00c01ad634df70cc7f67efba96b1503ffd63529c )

it's the crudepythonmodeler branch

README.md contains the install steps for ubuntu

4

3 に答える 3

1

I think I found the answer.

Here is the typedef for the ModelMap:

typedef ::std::map<Utf8String,Model*,Utf8StringComparator> ModelMap;

I had sloppily changed the signature in the Utf8StringComparator to return int instead of bool.

Thank you for looking into this. I'll confirm when I'm certain.

于 2012-11-27T01:03:45.657 に答える
1

Is probably a problem with ModelMap::iterator. You may want to revise how this was written.

于 2012-11-27T03:34:17.737 に答える
0

Assuming the above code prints the results quoted, it is pretty clear that the ModelMap data structure got corrupted in some way. The corruption almost certainly took place before this function was called. How the map got corrupted exactly is impossible to tell based on the quoted code and data. The two most likely candidates are

  1. The keys of some of values got changed (assuming the data structure is, indeed, a map).
  2. The internal pointers got overwritten at some point.

I would probably guess the latter although this normally results in a crash rather than some partial output. A potential reason could be that the object being looked at happens to be destroyed object returned by reference from a function. However, this is all pretty much guesswork: To locate the error, you'd need to post more code, removing a lot of the irrelevant code as well (i.e., posting hundreds lines of code won't give you an answer where the problem is exactly, nor will the above code, however).

于 2012-11-27T00:53:27.853 に答える