【发布时间】:2009-12-26 05:51:03
【问题描述】:
我有一些文件编写代码按预期工作,但在调试模式下打印错误,在发布时没有输出错误。
代码:
#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;
}
调试输出:
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
发布输出:
FileIO implementation C++
Please enter the filename to create: Running…
myfile
Created: myfile.txt
除了不检查打开的文件描述符(为简单起见)之外,这段代码有什么问题?
更新:我将代码分解为以下内容,但仍然出错:
string cppfilename;
getline(cin, cppfilename); // error here
【问题讨论】:
-
我怀疑这比您在此处描述的更多,代码看起来不错(但格式错误)。您可能想具体提及您正在使用的编译器版本。我不认为您尝试在 malloc_error_break 上设置断点?
-
这是整个例子吗?它对我来说运行良好(Windows 上的 Mingw g++)
-
是的。它也适用于我。我只是对为什么会收到错误感到困惑。
-
我确实在 malloc_error_break 上放了一个断点,但它没有在那里中断。
-
尝试将其分解为最小的失败代码。 IE。删除循环并只调用一次 getline,删除对文件的实际写入,等等。
标签: c++ xcode macos runtime-error