• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
felipedrl

Game Crash upon accessing files in Release Configuration

8 posts in this topic

Hi,

I'm struggling with a weird error which I can't figure out why it happens. So, once more, I ask for the aid of gamedev's wizards.

I'm using VisualStudio 2010 express edition and the game runs fine while in Debug configuration. When I run in Release configuration the game crashes upon the first access to a file (std::ifstream) giving an "access violation writing location" error message.

The error happens inside this function:
[code]

void __cdecl _lock_file (
FILE *pf
)
{
/*
* The way the FILE (pointed to by pf) is locked depends on whether
* it is part of _iob[] or not
*/
if ( (pf >= _iob) && (pf <= (&_iob[_IOB_ENTRIES-1])) )
{
/*
* FILE lies in _iob[] so the lock lies in _locktable[].
*/
_lock( _STREAM_LOCKS + (int)(pf - _iob) );
/* We set _IOLOCKED to indicate we locked the stream */
pf->_flag |= _IOLOCKED;
}
else
/*
* Not part of _iob[]. Therefore, *pf is a _FILEX and the
* lock field of the struct is an initialized critical
* section.
*/
EnterCriticalSection( &(((_FILEX *)pf)->lock) );
}
[/code]


I've found out that in Debug the execution path takes the [b]if[/b], while in Release it takes [b]else[/b]. My application does not create any additional thread.

My project configuration is the default "Empty Project" with the following changes:

Whole Program Optimization: No whole program optimization (By default it was using Link time code generation but this was giving me many linker error, so I turned it off)
Ignore Specific Library: msvcrt
SubsSystem: Windows

Does anyone have a clue of why this is happening?

Thanks in advance.
0

Share this post


Link to post
Share on other sites
It is unlikely that the cause is in the standard library and the code fragment you posted appears to be from there. Take a look at the call stack and analyze the last fragment of your code before you enter the standard library.

Considering the nature of the crash there is also a high probability you trash memory (for example by writing over array boundaries or accessing invalid pointers). These kinds of errors often don't surface in Debug builds for various reasons.
0

Share this post


Link to post
Share on other sites
http://msdn.microsoft.com/en-us/library/ya5wt12d.aspx
[quote]Comparison of pointers is guaranteed valid only when the pointers refer to objects in the same array or to the location one past the end of the array.[/quote]

If pf is not part of _iob the comparisons (pf >= _iob) and (pf <= (&_iob[_IOB_ENTRIES-1])) is undefined so you can't be sure of what will happen in that case.
-1

Share this post


Link to post
Share on other sites
It is possible that you are accessing an uninitialised variable somewhere. Debug runtimes initialise memory with set patterns, which can make your code behave consistently and appear correct. Once this is pulled from under you, the behaviour becomes inconsistent (memory is uninitialised) and your code can break, often in unrelated areas.

This isn't the only possible explanation. Ensure you're compiling with very high warning levels (good advice in general).
1

Share this post


Link to post
Share on other sites
Thanks for replying so fast.
[quote name='BitMaster' timestamp='1310570428' post='4834857']
It is unlikely that the cause is in the standard library and the code fragment you posted appears to be from there. Take a look at the call stack and analyze the last fragment of your code before you enter the standard library.

Considering the nature of the crash there is also a high probability you trash memory (for example by writing over array boundaries or accessing invalid pointers). These kinds of errors often don't surface in Debug builds for various reasons.
[/quote]

The code snipped is from file.c in standard lib. I just pasted it to show it fails the "if" in Release configuration. The last call to my application code is to read method from std::ifstream. It also crashes upon calling the "<<" operator. I think this error is not due to memory trash since I tried to access a file right after the application entry point and it still fails. The following code fails in Release:

[code]

int main(int argc, char *argv[])
{
// Added this here to test memory trash.
std::ifstream file("res/chart_01_easy.tws");
if (file.is_open())
{
unsigned int test = 0;
file.read(reinterpret_cast<char*>(&test), sizeof(unsigned int));
}

// Game code goes below
[/code]



[quote name='Wooh']
[color=#1C2837][size=2]If pf is not part of _iob the comparisons (pf >= _iob) and (pf <= (&_iob[_IOB_ENTRIES-1])) is undefined so you can't be sure of what will happen in that case.[/size][/color]
[/quote]

The question is why pf is not part of _iob buffer considering that all that I've done was to create a file object and tried to read a value from it, as you can see in the snippet above.
0

Share this post


Link to post
Share on other sites
[quote]
I think this error is not due to memory trash since I tried to access a file right after the application entry point and it still fails.
[/quote]
Do you have any global variables? What about the libraries you are using? What happens if you comment out all your game code?

If you create a new project, with just the following, and compile it in release mode:
[code]
int main(int argc, char *argv[])
{
// Added this here to test memory trash.
std::ifstream file("res/chart_01_easy.tws");
if (file.is_open())
{
unsigned int test = 0;
file.read(reinterpret_cast<char*>(&test), sizeof(unsigned int));
}
}
[/code]

[quote]
The question is why pf is not part of _iob buffer ...
[/quote]
That is [b]almost certainly[/b] not the actual problem. 99.999% of the time, when you're finding weird things in the standard library, it is caused by something your program is (or isn't) doing correctly.
1

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1310572991' post='4834879']
Do you have any global variables? What about the libraries you are using? What happens if you comment out all your game code?

If you create a new project, with just the following, and compile it in release mode:
[code]
int main(int argc, char *argv[])
{
// Added this here to test memory trash.
std::ifstream file("res/chart_01_easy.tws");
if (file.is_open())
{
unsigned int test = 0;
file.read(reinterpret_cast<char*>(&test), sizeof(unsigned int));
}
}
[/code]
[/quote]


Thanks for all the help!

A new project works.I'm checking right now all the things you pointed out. I'll post if I discover anything important or fix the problem.



0

Share this post


Link to post
Share on other sites
As the pointer is running outside of the array it sounds like you are not closing files.
0

Share this post


Link to post
Share on other sites
I've found out the problem but still don't know the reason. It was due to a project configuration. At some point in my development I had a warning complaining "msvcrt.lib" conflicting with other library. So I added it to "Ignore Specific Default Library" and Ta Da! It worked. The weird thing is that Debug has the same ignore and doesn't crash.

Another odd fact that happens is that the link fails when "Use Link Time Code Generation" option is activated. I can only link with "No Whole Program Optimization". The link fails regarding FTGL calls. Maybe it has something to do with name mangling since one of FTGL dependecies is truetype, which was compiled on C.

I don't know. My programming skills are too short for this problem. Maybe you could give me some tips or recommend a reading.

Thanks for all the support. You rock.




1

Share this post


Link to post
Share on other sites

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  
Followers 0