gpt4 book ai didi

c++ - Xcode STL C++调试编译报错

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:12:29 25 4
gpt4 key购买 nike

我有一些文件编写代码按预期工作,但在 Debug 模式下打印错误,在 Release 模式下没有输出错误。

代码:

#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

最佳答案

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

#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 中找到您的目标,双击它构建的可执行文件,转到构建选项卡,确保配置设置为调试,向下滚动到 GCC 4.2 - 预处理部分,并从 Preprocessor Macros 行中删除这两个值。通过这种方式,代码将构建并运行,并且在这种情况下似乎可以正常工作,但您会得到更少的断言,即标准库的调试构建可能已经能够捕捉到。

关于c++ - Xcode STL C++调试编译报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1962685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com