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

13
  • I doubt this is the problematic code. Nov 27, 2012 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, 2012 at 0:38
  • Can you post the whole thing if it is not much bigger?
    – imreal
    Nov 27, 2012 at 0:39
  • 3
    Just because the output changes when you change this code doesn't mean this code is the problem. Nov 27, 2012 at 0:39
  • 1
    The bug is almost certainly in code you haven't pasted. You can start by testing with valgrind. Nov 27, 2012 at 0:40

3 Answers 3

1

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

1
  • Ben help correct this error. See my answer for a full explanation.
    – Homer6
    Nov 27, 2012 at 4:08
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.

1
  • Confirmed. This was indeed the issue.
    – Homer6
    Nov 27, 2012 at 2:24
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).

2
  • 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, 2012 at 0:55
  • As I said, "posting hundreds lines of code won't give you an answer where the problem is exactly"! Nov 27, 2012 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.