Jump to content
• Advertisement

Public Group

# Unknown spontaneous error

This topic is 2476 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hello folks!

I'm slowly putting together another game and right now, I'm experiencing the weirdest bug I've seen yet! At first I thought it was a problem with how I was using std::map, but I've moved added a single line of code (call to a function), and now the code fails trying to call a constructor of a class.

Here's the basic rundown in the code:

 bool Level::SpawnEntity(std::string scriptname, Vector2D& pos) { static unsigned int lastID = 0; Entity* ent = new Entity(mLog); // After I added SpawnEntity to the Update function, this line started failing if(!ent->Spawn(mImageHeap, scriptname, pos)) { mLog->PrintError("Entity " + scriptname + " failed to load.\n\n"); mLog->WriteToFile(); return false; } std::cout << ent << std::endl; mEntityList.insert( std::pair<unsigned int, Entity*>(lastID++, ent)); return true; } int Level::Update() { const float frameTime = 1000/30; sf::Clock frameCounter; Vector2D pos(0.0,0.0); SpawnEntity("default", pos); // I have added this line to double check that only certain functions in std::map fail // I broke down the for loop into separate pieces to check where the code fails std::cout << "Counter Reset\n"; std::map<unsigned int, Entity*>::iterator it = mEntityList.begin(); // and it turns out it failed here, but I tried calling size() as well and that failed also std::cout << "iterator initialized\n"; while(it == mEntityList.end()) { std::cout << "iteration\n"; (*it).second->ResetCounter(); std::cout << "next iteration\n"; it++; } // I have yet to reach any of the code below } 

Now I honestly have no idea what to do. Can anyone give me an idea as to where to go from here?

#### Share this post

##### Share on other sites
Advertisement
My first guess would be that you're calling functions on a non-existent object, i.e. the Level class's this-pointer is bogus.

Of course, without any hint as to what the actual error is, that's just speculation at best ;-)

What does your debugger say the value of "this" is when the calls fail? And what is the actual "failure"?

#### Share this post

##### Share on other sites
If the code tends to break when you first touch a member of the class, it could indicate that the Level instance you are calling Update() on is invalid. Perhaps it has been deleted, or was never initialised to begin with. Worst case scenario you have memory corruption, and some other code could be writing over your class and invalidating its map.

Other than that, you haven't really explained the problem in enough detail. What does "it failed" mean? Did you get a runtime error? If so, what was the exact text of the error? Or were you expecting behaviour X, but observed behaviour Y? If so, be detailed about what behaviour X and Y consist of.

One way to debug really weird errors is to make a minimal example program that demonstrates the problem. Copy your code somewhere else, and start eliminating irrelevant code. Comment out the code bit by bit until it goes from failing to working, or until you have a short, one file program that succinctly shows the problems.

If you get it working, you can comment in the code you had just removed, and do a binary search by commenting out portions of that code until you get down to a few lines that isolate the problem. If you don't get it working, it should be down to a small program that you can paste here that we can compile and run and help you with.

#### Share this post

##### Share on other sites
 Entity* ent = new Entity(mLog); // After I added SpawnEntity to the Update function, this line started failing 

What is mLog? If Update is getting called before you set up mLog that might be the problem.

#### Share this post

##### Share on other sites
Wow, you guys are pros if I ever saw any. It turns out I was calling functions of a null pointer. It would have never come to my mind that such thing could happen. Turns out that when I created a Level instance I forgot to actually do anything with the pointer so I was calling the member functions on a bogus instance. I find it a little odd that the code didn't crash at calling the Update function but rather fails at trying to call functions from inside the Update functions.

Thanks guys!

#### Share this post

##### Share on other sites
Check out this article. It explains not only how your crash happened precisely the way it did, but how we're able to figure out what's going wrong without even seeing the error message ;-)

#### Share this post

##### Share on other sites
Cool thanks! I've actually seen the article before but obviously I haven't quite learned anything from it. I'll be sure to take notes this time around ^.^

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
frob
15
3. 3
4. 4
Rutin
12
5. 5
• Advertisement

• 12
• 12
• 58
• 14
• 15
• ### Forum Statistics

• Total Topics
632120
• Total Posts
3004219

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!