Microsoft Visual Studio C Runtime Library has detected a fatal error in......

Started by
10 comments, last by DarkRayne 14 years, 11 months ago
I am at the last assignment in this Michael Morrison Beginning game Programming book. It involves the Spaceout 3 game if any of you are familiar. But straight out of the book, it doesnt run correctly. After you hit enter after the splash screen, i get the immediate Microsoft Visual Studio C Runtime Library has detected a fatal error in SpaceOut 3.exe. Press Break to debug the program or Continue to terminate the program If i hit debug, it goes directly to line 65 in the dbhook.c locked file is anyone familiar with this type of error?
Advertisement
What does your call stack window look like when the crash happens? What CRT function are you calling from your code? Are you sure the parameters are correct?
This sort of thing is usually from using an invalid pointer - for instance trying to fread() from a FILE* that's already been fclose()d.
I copied and pasted the called stack window i found. Is this the list of errors that were found?




> SpaceOut 3.exe!_crt_debugger_hook(int _Reserved=4194304) Line 65 C
SpaceOut 3.exe!_invalid_parameter(const wchar_t * pszExpression=0x00000000, const wchar_t * pszFunction=0x00000000, const wchar_t * pszFile=0x00000000, unsigned int nLine=0, unsigned int pReserved=0) Line 86 + 0x7 bytes C++
SpaceOut 3.exe!_invalid_parameter_noinfo() Line 99 + 0xc bytes C++
SpaceOut 3.exe!GameEngine::CleanupSprites() Line 408 + 0xa bytes C++
SpaceOut 3.exe!NewGame() Line 375 C++
SpaceOut 3.exe!HandleKeys() Line 246 C++
SpaceOut 3.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * szCmdLine=0x00262bd3, int iCmdShow=1) Line 55 C++
SpaceOut 3.exe!__tmainCRTStartup() Line 324 + 0x1c bytes C
kernel32.dll!77024911()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!77c8e4b6()
ntdll.dll!77c8e489()

Quote:Original post by DarkRayne
I copied and pasted the called stack window i found. Is this the list of errors that were found?




> SpaceOut 3.exe!_crt_debugger_hook(int _Reserved=4194304) Line 65 C
SpaceOut 3.exe!_invalid_parameter(const wchar_t * pszExpression=0x00000000, const wchar_t * pszFunction=0x00000000, const wchar_t * pszFile=0x00000000, unsigned int nLine=0, unsigned int pReserved=0) Line 86 + 0x7 bytes C++
SpaceOut 3.exe!_invalid_parameter_noinfo() Line 99 + 0xc bytes C++
SpaceOut 3.exe!GameEngine::CleanupSprites() Line 408 + 0xa bytes C++
SpaceOut 3.exe!NewGame() Line 375 C++
SpaceOut 3.exe!HandleKeys() Line 246 C++
SpaceOut 3.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * szCmdLine=0x00262bd3, int iCmdShow=1) Line 55 C++
SpaceOut 3.exe!__tmainCRTStartup() Line 324 + 0x1c bytes C
kernel32.dll!77024911()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!77c8e4b6()
ntdll.dll!77c8e489()
If you click on the "GameEngine::CleanupSprites" line, can we see the code around the error (And the line that seems to be calling _invalid_parameter_noinfo)?
The area is
void GameEngine::CleanupSprites(){  // Delete and remove the sprites in the sprite vector  vector<Sprite*>::iterator siSprite;  for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++)  {    delete (*siSprite);    m_vSprites.erase(siSprite);    siSprite--;  }}



The specific line theat it goes to is

siSprite--;
That's a pretty strange way of clearing a vector. Do you still get the crash if you do this instead?
void GameEngine::CleanupSprites(){  for(vector<Sprite*>::iterator it=m_vSprites.begin(); it!=m_vSprites.end(); ++it)    delete (*it);   m_vSprites.clear();}
I suspect that your crash if because on the first iteration of your loop, you're trying to decrement the iterator from begin(), which is illegal.
Your iterator was probably invalidated. You should do something like this when deleting an element from a container:

siSprite = m_vSprites.erase(siSprite);

However, if you are using a std::vector of sprites and not a std::list then in this case, you should probably do something more like this:

void GameEngine::CleanupSprites(){  // Delete and remove the sprites in the sprite vector  vector<Sprite*>::iterator siSprite;  for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++)  {    delete (*siSprite);  }  m_vSprites.clear();}


If you're going to call delete on every sprite you should probably hold off on deleting the pointer itself from the container until you finish. If you were using a vector then every time you deleted an element all of the elements after that would have to be moved to fill in the space. Very slow! With a std::list it is probably faster your way. Hope this helps.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

You are the man,pure genius. I replaced the function with

void GameEngine::CleanupSprites()
{
for(vector<Sprite*>::iterator it=m_vSprites.begin(); it!=m_vSprites.end(); ++it)
delete (*it);

m_vSprites.clear();
}

and it worked. Thank you soo much. Would the original code have ever worked?
Also, is looking at the call stack area another way of seeing the errors/debugging.
Quote:Original post by DarkRayne
Would the original code have ever worked?
On VC6 (And possibly VC2003)'s default STL implementation it'd work because vector iterators were just pointers - so decrementing a pointer to one before the start of the array is fine so long as you don't dereference it, and the pointer is then incremented back to the first element at the top of the for() loop.

Quote:Original post by DarkRayne
Also, is looking at the call stack area another way of seeing the errors/debugging.
It's the best way to determine what line of your code is causing the crash, if the crash happens in some CRT or library code.

If you haven't already, I'd recommend taking a look over Superpig's tutorial on debugging.
What is CRT?

Also, do you game program as a hobby or employed as a programmer? This site is a wealth of knowlegde. But i feel like i started off on the wrong foot. I began in C++ as a first language (feel like im almost drowning). I have taken a few online classes up to data structures, it sort of seems as though the school just threw you to the wolves. Should i have started with VB instead?

This topic is closed to new replies.

Advertisement