I have some file writing code that works as expected, but prints an error on Debug mode, no output errors in Release.
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main (int argc, char * const argv[]) {
string cppfilename;
std::cout << "Please enter the filename to create: ";
while ( cppfilename == "" ) {
getline(cin, cppfilename); // error occurs here
}
cppfilename += ".txt";
ofstream fileout;
fileout.open( cppfilename.c_str() );
fileout << "Writing this to a file.\n";
fileout.close();
return 0;
}
Debug Output:
Please enter the filename to create: Running…
myfile
FileIO(5403) malloc: *** error for object 0xb3e8: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Created: myfile.txt
Release Output:
FileIO implementation C++
Please enter the filename to create: Running…
myfile
Created: myfile.txt
Aside from not checking for the file descriptor being open (for simplicity) what is wrong with this code?
Update: I broke the code down to the following and it still errors:
string cppfilename;
getline(cin, cppfilename); // error here
This looks to be another case of _GLIBCXX_DEBUG being broken with gcc 4.2 on Mac OS X.
Your best options look to be to drop _GLIBCXX_DEBUG or to switch to gcc 4.0.