Take the 2-minute tour ×

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;



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

$ program

$ program

$ program

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...


Committed entire codebase for debugging:


commit ( 00c01ad634df70cc7f67efba96b1503ffd63529c )

it's the crudepythonmodeler branch

README.md contains the install steps for ubuntu

share|improve this question
I doubt this is the problematic code. –  Luchian Grigore Nov 27 '12 at 0:37
I've checked it multiple times. And when I change the code, the output changes. This is certainly the problematic code. –  Homer6 Nov 27 '12 at 0:38
Can you post the whole thing if it is not much bigger? –  imreal Nov 27 '12 at 0:39
Just because the output changes when you change this code doesn't mean this code is the problem. –  Luchian Grigore Nov 27 '12 at 0:39
The bug is almost certainly in code you haven't pasted. You can start by testing with valgrind. –  David Schwartz Nov 27 '12 at 0:40

3 Answers 3

up vote 1 down vote accepted

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

share|improve this answer
Ben help correct this error. See my answer for a full explanation. –  Homer6 Nov 27 '12 at 4:08

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

share|improve this answer
Thanks for the response. I've added the entire codebase to github. I'll check it over for those specific errors too. –  Homer6 Nov 27 '12 at 0:55
As I said, "posting hundreds lines of code won't give you an answer where the problem is exactly"! –  Dietmar Kühl Nov 27 '12 at 0:58

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.

share|improve this answer
Please read sscce.org and try to follow it when posting questions. –  Jesse Good Nov 27 '12 at 1:08
Will do. Thank you. –  Homer6 Nov 27 '12 at 1:10
Confirmed. This was indeed the issue. –  Homer6 Nov 27 '12 at 2:24

Your Answer


