2009-12-26 88 views
4

我有一些文件编写代码,按预期方式工作,但在调试模式下打印错误,在发布中没有输出错误。Xcode STL C++调试编译错误

代码:

#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 
从n个

除了检查文件描述符是否被打开(为了简单起见)这段代码有什么问题?

更新:我打破了代码下面的这个,它仍然错误:

string cppfilename; 
getline(cin, cppfilename); // error here 
+1

我怀疑这里比你在这里描述的更多,代码看起来很好(但格式不正确)。你可能想特别提一下你使用的编译器版本。我不假设你已经尝试在malloc_error_break上放置一个断点? – 2009-12-26 06:03:30

+1

这是整个例子吗?它运行良好(Windows上的Mingw g ++) – 2009-12-26 06:05:01

+0

是的。它对我来说也很好。我只是困惑,为什么我得到的错误。 – 2009-12-26 06:06:30

回答

5

这看起来是_GLIBCXX_DEBUG being broken with gcc 4.2 on Mac OS X另一种情况。

你最好的选择是放弃_GLIBCXX_DEBUG或切换到gcc 4.0。

+0

嗯,该死的。所有这些时间来调查和写出一个长期的,详细的答案,并且恰巧在这里有另一个答案,它说,这是一个已知的问题。我现在感到很傻。 – 2009-12-26 08:42:06

+0

我将Xcode中的编译器更改为4.0,并解决了问题。非常感谢 – 2009-12-29 04:04:26

6

这看起来像我在Apple的libstdc++中的一个bug,至少在调试模式下编译时。如果我编译你上面给两行减少:

#include <iostream> 
#include <string> 

using namespace std; 

int main() { 
    string cppfilename; 
    getline(cin, cppfilename); // error here 

    return 0; 
} 

用下面的命令行(从Xcode的默认设置采取在C调试版本++项目定义):

g++ -D_GLIBCXX_DEBUG=1 -D_GLIBCXX_DEBUG_PEDANTIC=1 -g -o getline getline.cpp 

然后我让你看到了同样的错误:

$ ./getline foo 
getline(74318) malloc: *** error for object 0x1000021e0: pointer being freed was not allocated 
*** set a breakpoint in malloc_error_break to debug 
Abort trap 

这个弹出崩溃报告,这给了我们一个堆栈跟踪(你也可以通过Xcode的下运行此获得从调试器堆栈跟踪;我只是想重现它在清洁的环境中成为可能,试图找出原因,没有别的什么奇怪的Xcode可能会做):

Thread 0 Crashed: Dispatch queue: com.apple.main-thread 
0 libSystem.B.dylib    0x00007fff83c37fe6 __kill + 10 
1 libSystem.B.dylib    0x00007fff83cd8e32 abort + 83 
2 libSystem.B.dylib    0x00007fff83bf0155 free + 128 
3 libstdc++.6.dylib    0x00007fff813e01e8 std::string::reserve(unsigned long) + 90 
4 libstdc++.6.dylib    0x00007fff813e0243 std::string::push_back(char) + 63 
5 libstdc++.6.dylib    0x00007fff813c92b5 std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char) + 277 
6 getline       0x00000001000011f5 std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) + 64 (basic_string.h:2451) 
7 getline       0x0000000100000cbf main + 34 (getline.cpp:10) 
8 getline       0x0000000100000c04 start + 52 

这看起来极像是对我的错误。我们正在使用一些标准的库函数,尽可能使用最简单的方法,并触发断言失败。我们不得不放弃file a bug report with our vendor,并尝试寻找解决方法。如果我们使用的是专有软件(Apple的许多软件是幸运的,但幸运的是libstdc++是自由软件),我们将不得不放弃file a bug report with our vendor,并尝试寻找解决方法。幸运的是,这是免费软件,所以我们可以调查根本原因。不幸的是,我目前没有时间来追踪这个问题的根源,但是请仔细阅读source is available

您应该可能file a bug about this。在这种情况下,解决方法是删除_GLIBCXX_DEBUG = 1定义(也可能是_GLIBCXX_DEBUG_PEDANTIC = 1)。你可以在Xcode中找到你的Target,双击它生成的可执行文件,进入Build选项卡,确保配置设置为Debug,滚动到GCC 4的。2 - 预处理部分,并从预处理器宏行中删除这两个值。通过这种方式,代码将生成并运行,并且在这种情况下似乎可以工作,但是您会收到更少的断言,标准库的调试版本可能已经能够捕获。

+0

谢谢你的全面回答Brian。我必须给你+1 – 2009-12-29 03:55:19