Thread: Crash on ofstream declaration...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up Crash on ofstream declaration...

    I'm making a sort of I/O wrapper for a larger project, but all of a sudden this crash has appeared. I have narrowed it down to this line:

    Code:
    std::ofstream test;
    Not very elaborate, eh?

    The thing is, this line was called a few times before, and worked fine. But after a certain amount of times, if crashes with this message:

    "Unhandled exception at 0x76b8b727 in fileconvert.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0035f2c0.."

    And it directs me to line 376 in 'mlock.c'. I have done extensive debugging and can't find the problem. Can anyone point me in the right direction?

    Cheers.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps the problem lies elsewhere (e.g., undefined behaviour due to buffer overflow), but merely is manifested at this line.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Is test declared at the beginning of a new scope? Maybe one of your trailing destructors made a mess.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    The code I gave you before was test code (obviously) but here is the actuall code:
    Code:
    FILEIOASCII::FILEIOASCII( std::string& Filename, __int8 OpenType, bool Append ) : OpenType( OpenType )
    {
    	iFile = NULL;
    	oFile = NULL;
    
    	if ( OpenType == IO_READ )
    		iFile = new std::ifstream( Filename.c_str() ); // crash here
    
    	else if ( OpenType == IO_WRITE )
    	{
    		oFile = new std::ofstream( Filename.c_str(), ( Append ? std::ios::app : std::ios::trunc ) ); // crash here
    	}
    
    	else
    		WNG( "File has incorrect open type." );
    };
    I also noticed that it crashes when doing an ifstream as well (I pointed it out in the above code.

    What I have done is I have called this code once (I create an object with this constructor). Then when I call it again, I get an error.
    Also, if I call this constructor once, then I try to create and o/ifsteam, it still crashes. So this code (or somewhere else) is affecting fstream somehow.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by yaya
    What I have done is I have called this code once (I create an object with this constructor). Then when I call it again, I get an error.
    Show this part of your code. Also, did you disable the the copy constructor and copy assignment operator of FILEIOASCII? Did you implement the destructor correctly? Why are you storing pointers to std::ifstream and std::ofstream in the first place?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Quote Originally Posted by laserlight View Post
    Show this part of your code.
    There isn't really much to show... but:
    Code:
    // first time
    FILEIOASCII File( Filename, IO_READ );
    
    // second time
    FILEIOASCII File( Filename, IO_WRITE );
    Also, did you disable the the copy constructor and copy assignment operator of FILEIOASCII? Did you implement the destructor correctly? Why are you storing pointers to std::ifstream and std:fstream in the first place?
    I tried changing the pointers to non-pointers and the problem still happened. There are no other pointers in the class so I don't think a copy constructor/copy assignment operator would be the cause.

    Just to clear things up, here's the rest of the code for that class:

    Code:
    class FILEIOASCII
    {
    public:
    	FILEIOASCII( std::string& Filename, __int8 OpenType, bool Append = false );
    	~FILEIOASCII();
    
    	template < typename T > bool Action( T& Data );
    	bool IsEOF();
    
    private:
    	std::ifstream iFile;
    	std::ofstream oFile;
    
    	__int8 OpenType;
    };
    
    template < typename T > bool FILEIOASCII::Action( T& Data )
    {
    	if ( OpenType == IO_READ )
    	{
    		iFile >> Data;
    		return true;
    	}
    
    	else if ( OpenType == IO_WRITE )
    	{
    		oFile << Data;
    		oFile << ' ';
    		return true;
    	}
    
    	WNG( "File has incorrect open type." );
    	return false;
    };
    
    FILEIOASCII::~FILEIOASCII()
    {
    	if ( OpenType == IO_READ )
    		iFile.close();
    	else if ( OpenType == IO_WRITE )
    		oFile.close();
    	else
    		WNG( "File has incorrect open type." );
    };
    
    bool FILEIOASCII::IsEOF()
    {
    	return OpenType == IO_READ ? iFile.eof() : oFile.eof();
    };

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by yaya
    There isn't really much to show... but:
    How did you manage to use the name File for two different variables in the same scope?

    Anyway, I tested with this:
    Code:
    #include <string>
    #include <fstream>
    #include <cassert>
    
    class FileIoAscii
    {
    public:
        enum IoType {io_read, io_write};
    
        FileIoAscii(const std::string& name, IoType io_type_)
            : io_type(io_type_)
        {
            if (io_type == io_read)
            {
                in.open(name.c_str());
            }
            else if (io_type == io_write)
            {
                out.open(name.c_str());
            }
            else
            {
                assert(!"Invalid I/O type.");
            }
        }
    private:
        std::ifstream in;
        std::ofstream out;
    
        IoType io_type;
    
        FileIoAscii(const FileIoAscii&);
        void operator=(const FileIoAscii&);
    };
    
    int main()
    {
        const std::string filename("test.c");
        FileIoAscii one(filename, FileIoAscii::io_read);
        FileIoAscii two(filename, FileIoAscii::io_write);
    }
    Frankly, it works as expected, i.e., the program did not crash. Therefore, from what you showed, the problem most likely lies elsewhere, in the code that you did not show.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Oh, they weren't in the same scope. I was just simplifying code. I guess I'll just keep searching. Thanks for the help.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by yaya
    Oh, they weren't in the same scope. I was just simplifying code.
    It is good to simplify, but the simplification must still demonstrate the problem, otherwise it will not reveal anything (other than a better way of doing things, if you're lucky).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM