Sign in to follow this  

Strange ifstream crash

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

Stack:

 	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;
  
  Debug::Log(path);
  std::ifstream file(path, std::ifstream::in);
  std::stringstream readIn;

  if (file)
  {
    readIn << file.rdbuf(); // CRASH
    file.close();
  }
  else
    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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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