Sign in to follow this  

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

This topic is 3152 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

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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()

Share this post


Link to post
Share on other sites
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)?

Share this post


Link to post
Share on other sites
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--;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by DarkRayne
What is CRT?
CRT stands for C RunTime library, and is the library that your application uses to interface with the Operating System. Functions like fopen(), printf(), time(), rand(), malloc() etc are all part of the CRT.
The point in the CRT is to give your application a standard interface so that your code can (in theory) be compiled on Windows, Linux, Max OS, or any other OS with a compiler supporting a CRT. Without it, you'd have to make OS calls like HeapAlloc, WriteFile, GetSystemTime, and so on, and you'd need to write code for any other OS you want to compile your code on.

Quote:
Original post by DarkRayne
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?
I started as a hobbyist, I'm now working in the industry.
I started off with BASIC on the ZX Spectrum, then STOS BASIC on the Atari ST, then a bit of Pascal at school. I then jumped ahead a bit and learnt Quake C and C (Learning both at once was pretty confusing...), and then C++.
C++ really isn't that good a language for beginners really - it's very low level and full of annoying gotchas (Like this one for instance). VB, Java, or C# might be a better starting choice.
Having said that, there's nothing to say that you can't start with C++, so long as you take it slowly and make sure you understand everything you're doing, rather than just copy and pasting code and having it work but you're not sure why. If you're unsure about why something works, we at GameDev.Net will be more than happy to answer any questions.

Share this post


Link to post
Share on other sites
I wish I found this site before I started my classes. You live and you learn. I am really looking to get into the business, Im currently in the military , so I wont have the experience as many will have. Im gonna have to really get this down to catch up. I hope that I am not the only one out there that can spend like 5-7 hours staring at the screen and get absolutely no where. Its a little frustrating.

But anyway ,thank you to all for the help. I really appreciate it.

Share this post


Link to post
Share on other sites

This topic is 3152 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this