Jump to content

  • Log In with Google      Sign In   
  • Create Account

Strange ifstream crash

  • You cannot reply to this topic
5 replies to this topic

#1 Oammar   Members   -  Reputation: 162


Posted 23 August 2014 - 06:42 PM

I've been working on my own library in a different project. I decided to create a new project so I can test out what I've done so far in a fresh work space.


The issue I am encountering begins when I am attempting to open a file for reading from. It does open the file, however, when I attempt to read from it, or do any file related operations it crashes with 0xC0000005 exception, leading me on the stack to: EnterCriticalSection


I'm on Win7 using Visual Studio 2012


Message: Unhandled exception at 0x77E48E19 (ntdll.dll) in MainProj.exe: 0xC0000005: Access violation writing location 0x00000014.


 	ntdll.dll!77e48e19()	Unknown
 	[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]	
 	ntdll.dll!77e48d28()	Unknown
 	msvcr110.dll!_lock_file(_iobuf * pf) Line 223	C
 	msvcr110.dll!getc(_iobuf * stream) Line 43	C
 	MainProj.exe!std::_Fgetc<char>(char & _Byte, _iobuf * _File) Line 39	C++
 	MainProj.exe!std::basic_filebuf<char,std::char_traits<char> >::uflow() Line 482	C++
 	MainProj.exe!std::basic_filebuf<char,std::char_traits<char> >::underflow() Line 460	C++
 	msvcp110d.dll!std::basic_streambuf<char,std::char_traits<char> >::sgetc() Line 153	C++
 	msvcp110d.dll!std::basic_ostream<char,std::char_traits<char> >::operator<<(std::basic_streambuf<char,std::char_traits<char> > * _Strbuf) Line 513	C++
>	MainProj.exe!Utilities::ParseAndCollectFile(std::basic_string<char,std::char_traits<char>,std::allocator<char> > filePath) Line 44	C++

This is what I am doing:

std::string Utilities::ParseAndCollectFile(std::string filePath)
  char cCurrentPath[FILENAME_MAX];
  _getcwd(cCurrentPath, sizeof(cCurrentPath));
  std::string path = cCurrentPath;
  path += '\\';
  path += filePath;
  std::ifstream file(path, std::ifstream::in);
  std::stringstream readIn;

  if (file)
    readIn << file.rdbuf(); // CRASH
    Debug::Log("Failed To Open File: " + filePath);

  return readIn.str();

I've check to make sure the file is in the right location. However, another strange issue is: if I do:

std::ifstream file("data/maps/exportedForestMap.txt", std::ifstream::in);

before calling the library function that collects the file string data, the loading of the file problem goes away. I'm not using any custom threads at this point, this problem is baffling, everything works as it should in my main project where I'm working on the library.


Thank you for any insight you can give me as to why this is happening and various ways to solve the problem.

Edited by Oammar, 23 August 2014 - 07:08 PM.


#2 Oammar   Members   -  Reputation: 162


Posted 23 August 2014 - 08:30 PM

What a strange situation. I was able to 'fix' this issue, but I am not satisfied with the situation at all..


In my main project I did not have to change any of these settings, I am not sure at all why I needed to change these settings for this new project to get this to work properly.


I had to change the Runtime Library setting to use Multi-threaded DLL (/MD) and then add: msvcrtd.lib to my dependencies, that solved my problems I was encountering for building in debug mode.

#3 Erik Rufelt   Crossbones+   -  Reputation: 3262


Posted 23 August 2014 - 08:41 PM

Is all the code in the same new project and being compiled together, or are you including any code you've built as a library?

That could cause strange issues if they don't use the same settings, which fits how you fixed it.


Other similar issues could arise from different files/libraries being compiled with different preprocessor-macros or similar.

#4 Zaoshi Kaba   Crossbones+   -  Reputation: 4089


Posted 24 August 2014 - 01:26 AM

This problem can occur if library and executable use different Runtime Library (DLL vs Static, and Debug vs Release). Make sure it matches across all projects and do full recompile.

#5 Olof Hedman   Crossbones+   -  Reputation: 2738


Posted 24 August 2014 - 02:41 AM

Just a note, since this seems to be resolved already, but "if(file)" is not a proper way to check if the file opened or not.

I'm guessing the file actually fails to open


file will always be non-null, and you should use "if(file.is_open())" to see if it really is open, (or "if(file.fail())" to see if it failed)


Edit: oops, seems I need to read the spec more carefully before answering :P

Edited by Olof Hedman, 24 August 2014 - 03:39 PM.

#6 Mona2000   Members   -  Reputation: 590


Posted 24 August 2014 - 07:14 AM

Just a note, since this seems to be resolved already, but "if(file)" is not a proper way to check if the file opened or not.


It actually is, using operator void * (or operator bool if c++11).