代码之家  ›  专栏  ›  技术社区  ›  Brock Woolf

XC++STL C++调试错误编译错误

  •  4
  • Brock Woolf  · 技术社区  · 16 年前

    代码:

    #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
    
    2 回复  |  直到 16 年前
        2
  •  6
  •   Brian Campbell Dennis Williamson    16 年前

    libstdc++ ,至少在调试模式下编译时。如果我编译您上面给出的两行缩减:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
      string cppfilename; 
      getline(cin, cppfilename);    // error here
    
      return 0;
    }
    

    使用以下命令行(在C++项目中,使用XCODE对调试生成的默认设置进行定义):

    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 ,并尝试找到解决方法。幸运的是,这是免费软件,所以我们可以调查根本原因。不幸的是,我现在没有时间追根究底,但是 source is available 供阅读。

    file a bug about this . 这种情况下的一个解决方法是删除_GLIBCXX_DEBUG=1定义(可能还有_GLIBCXX_DEBUG_PEDANTIC=1)。您可以在Xcode中找到您的目标,双击它构建的可执行文件,转到build选项卡,确保配置设置为Debug,向下滚动到 GCC 4.2-预处理 节,并从 预处理宏