• Create Account

## Game Crash upon accessing files in Release Configuration

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1Felipe Lira  Members

Posted 13 July 2011 - 08:59 AM

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:

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) );
}


I've found out that in Debug the execution path takes the if, while in Release it takes else. 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?

### #2BitMaster  Members

Posted 13 July 2011 - 09:20 AM

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.

### #3Wooh  Members

Posted 13 July 2011 - 09:22 AM

http://msdn.microsoft.com/en-us/library/ya5wt12d.aspx

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.

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.

### #4rip-off  Moderators

Posted 13 July 2011 - 09:29 AM

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).

### #5Felipe Lira  Members

Posted 13 July 2011 - 09:42 AM

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.

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:


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;
}

// Game code goes below


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.

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.

### #6rip-off  Moderators

Posted 13 July 2011 - 10:03 AM

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.

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:
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;
}
}


The question is why pf is not part of _iob buffer ...

That is almost certainly 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.

### #7Felipe Lira  Members

Posted 13 July 2011 - 01:03 PM

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:

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;
}
}


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.

### #8seedy  Members

Posted 13 July 2011 - 01:14 PM

As the pointer is running outside of the array it sounds like you are not closing files.

### #9Felipe Lira  Members

Posted 20 July 2011 - 10:15 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.