0

The following function works fine, showing a text file line by line to stderr:

void load_text(std::string path){
  ifstream f(path);
  char linebuff[100];
  linebuff[99] = 0;
  while(!f.eof()){
    f.getline(linebuff, 99);
    std::cerr<<linebuff<<std::endl;
  }
  f.close();
}

Now, when the main function returns, it throws the following acces violation error:

Unhandled exception at 0x77e58dc9 in app.exe: 0xC0000005: Access violation writing location 0x00000014.

Oddly enough, creating an ifstream, closing it and returning also throws the error

//This also crashes when returning from main
void load_text(std::string path){
  ifstream f(path);
  f.close();
}

Any idea why this happens?

Edit:

The main function (as it is compiled), this actually works provided you create a new project, the difference with the actual program are a lot of never called, never used functions and classes

Right now I'm in the 'cannot reproduce' stage:

#include <fstream>
#include <string>
#include <iostream>

//Using SDL for plotting
#ifdef WIN32
  #pragma comment(lib, "SDL")
  #pragma comment(lib, "SDLMain")
  #pragma comment(lib, "SDL_image")
#endif

int fn(std::string path){
    std::ifstream f(path);
    char linebuff[100];
    linebuff[99] = 0;
    while(!f.eof()){
      f.getline(linebuff, 99);
      std::cerr<<linebuff<<std::endl;
    }
    f.close();
    return 0;
}

int main(int argc, char** argv){
    fn("sscce.cpp");
    return 0;
}
NeonMan
  • 583
  • 9
  • 21
  • [`while (!eof())` is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). And why are you reading in C strings? – chris Jun 05 '13 at 01:58
  • 1
    Apart from being a little different than what I would do (`std::string line` and `while (std::getline(f, line))` for the loop condition, `f.eof()` is almost never what you want to do), this looks ok (esp the latter). You say the exception is after `main()` returns, but don't bother showing us what is in `main()` on either side of this proc call. Please post a [SSCCE](http://www.sscce.org) including a `main()` that reproduces the problem? – WhozCraig Jun 05 '13 at 02:01
  • Trying to reproduce on a sample program. the main is identical to the one provided on the edit, so is the function, but this one does not throw any error. #pragmas can be deleted. – NeonMan Jun 05 '13 at 02:29
  • Solved. A new MSVC oddity to the list. – NeonMan Jun 05 '13 at 03:11
  • Your `#ifdef` is wrong, it should be `#ifdef _WIN32`. Note the leading underscore. – Blastfurnace Jun 05 '13 at 04:17
  • Changed the ifdefs (or removing them alltogether) does not change the program error, it was solved for some reason by removing the libraries-on-pragmas. – NeonMan Jun 06 '13 at 18:32

1 Answers1

0

For some reason, telling the compiler to use libraries using pragmas is not he same as explicitly setting them in te configuration. By removing the pragmas and putting the .lib files on the linker options it works.

found via: http://www.gamedev.net/topic/600901-lock-file-access-violation/

NeonMan
  • 583
  • 9
  • 21