Xcode STL C++ Debug 编译错误

Posted

技术标签:

【中文标题】Xcode STL C++ Debug 编译错误【英文标题】:Xcode STL C++ Debug compile error 【发布时间】: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,删除对文件的实际写入,等等。 【参考方案1】:

在我看来,这就像 Apple 的 libstdc++ 中的一个错误,至少在调试模式下编译时是这样。如果我编译你上面给出的两行缩减:

#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

在我看来,这非常像一个错误。我们以最简单的方式使用了一些标准库函数,但遇到了断言失败。

此时,如果我们使用的是专有软件(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,双击它构建的可执行文件,转到构建选项卡,确保配置设置为调试,向下滚动到 GCC 4.2 - 预处理 部分,并从 Preprocessor Macros 行中删除这两个值。这样代码将构建和运行,并且在这种情况下似乎可以工作,但您会得到更少的断言,标准库的调试版本可能已经能够捕获。

【讨论】:

哇,谢谢布莱恩这么全面的回答。我必须给你 +1【参考方案2】:

这似乎是_GLIBCXX_DEBUG being broken with gcc 4.2 on Mac OS X 的另一种情况。

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

【讨论】:

好吧,该死的。所有这些时间都在调查并写出一个长而详细的答案,并且碰巧这里有另一个答案说这是一个已知问题。我现在觉得很傻。 我将 Xcode 中的编译器更改为 4.0 并解决了问题。非常感谢

以上是关于Xcode STL C++ Debug 编译错误的主要内容,如果未能解决你的问题,请参考以下文章

c++程序不同环境就很卡

c ++:在c ++ STL中使用map编译错误? [关闭]

iOS11+Xcode9适配踩坑大全(下拉刷新,编译错误, Swift第三方等)

在xcode中编译ios项目时找不到c++头文件

在 Cython 中使用 C++ STL 映射

C++ Xcode iOS 项目错误