0
votes

I don't know what to make of this. I've written a function that reads a .obj file, not unlike the dozens of other example functions out there for processing information from a text file. I've included fstream, iostream, and sstream. It compiles. Yet when I run it I get:

  • An Unhandled Exception at a memory address (ntdll.dll) that complains about access violation writing location (I'm reading, not writing...).

  • My variable watch on "ifstream myfile" reads identifier "myfile" is undefined". If I place a break on the "ifstream myfile(...)" line it reads "Unable to read memory" instead. Error reading characters of string also occurs just prior to the Unhandled Exception.

  • Execution stops here in fstream during the getline call:

    virtual void __CLR_OR_THIS_CALL _Lock()
    {   // lock file instead of stream buffer
    if (_Myfile)
        _CSTD _lock_file(_Myfile);
    }
    

Relevant code, not much to see... pretty straight forward stuff. "file" is a const char* that reads "C:\cube.obj". Using namespace std.

ifstream myfile(file, ios::in);

if (myfile.is_open())
{
    if (myfile.good())
    {
        string line;
        while (std::getline(myfile, line))
        {
            // Foo
        }
    }
}

myfile.close();

I don't understand how on earth myfile is undefined despite straight up declaring it. fstream is clearly the right include and is accessible. The file is where it should be.

How can I debug this further? Teach me, oh wise ones. Using C++11 with Visual Studio 2013.

1
C:\ directory may be inaccessible to your program's privilege level. Try putting cube.obj in, say, Documents.IllusiveBrian
From what I remember, you need to step past the declaration line before it will have the information on the created object.chris
Btw you don't need to close the ifstream, the destructor will do it.Étienne
@Stradigos: try enabling exceptions on your ifstream object, that might help pinpoint the problem. See en.cppreference.com/w/cpp/io/basic_ios/exceptions and an example here: coliru.stacked-crooked.com/a/95a11d0599a925ca (note that Visual Studio error messages are more helpful)slaphappy
@Stradigos In that case, the error has happened before you even got to this code. But the getline will fail if an out-of-bounds array access (or use of a dangling pointer) somewhere (anywhere) overwrites the stream object. The "Unable to read memory" is usually an indication that an object has become corrupt.molbdnilo

1 Answers

0
votes

SOLVED! I knew it wasn't pointer related. Found this post, tried what it said, and it works flawlessly now. MSVCRTD.lib was missing in my dependencies. Why that's not mentioned anywhere as a prereq for using fstream is crazy. Can't believe the includes aren't enough.

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

http://www.gamedev.net/topic/660218-strange-ifstream-crash/