Xcode STL C++调试编译错误

发布于 2024-08-16 01:51:42 字数 1255 浏览 6 评论 0原文

我有一些文件编写代码可以按预期工作,但在调试模式下打印错误,在发布模式下没有输出错误。

代码:

#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

I have some file writing code that works as expected, but prints an error on Debug mode, no output errors in Release.

Code:

#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;
}

Debug Output:

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

Release Output:

FileIO implementation C++
Please enter the filename to create: Running…
myfile
Created: myfile.txt

Aside from not checking for the file descriptor being open (for simplicity) what is wrong with this code?

Update: I broke the code down to the following and it still errors:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爺獨霸怡葒院 2024-08-23 01:51:42

在我看来,这就像 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++ 是免费软件),我们将不得不放弃,向我们的供应商提交错误报告,并尝试找到解决方法。幸运的是,这是免费软件,因此我们可以调查根本原因。不幸的是,我现在没有时间追查根本原因,但是 源代码可供细读

您可能应该提交有关此问题的错误。这种情况下的解决方法是删除 _GLIBCXX_DEBUG=1 定义(也可能删除 _GLIBCXX_DEBUG_PEDATIC=1 )。您可以在 Xcode 中执行此操作,方法是找到目标,双击它构建的可执行文件,转到“构建”选项卡,确保配置设置为“调试”,向下滚动到“GCC 4.2 - 预处理”部分,并从预处理器宏行中删除这两个值。这样,代码将构建并运行,并且在这种情况下似乎可以工作,但是您将获得更少的标准库的调试构建可能能够捕获的断言。

This looks to me like a bug in Apple's libstdc++, at least when compiled in debug mode. If I compile the two line reduction you gave above:

#include <iostream>
#include <string>

using namespace std;

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

  return 0;
}

With the following command line (with defines taken from Xcode's default settings for a Debug build in a C++ project):

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

Then I get the same error that you saw:

$ ./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

This pops up a crash report, which gives us a stack trace (you can also get a stack trace from the debugger by running this under Xcode; I just wanted to reproduce it in as clean an environment as possible, to try and isolate the cause, without anything else strange Xcode might be doing):

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

This looks an awful lot like a bug to me. We're using some standard library functions in the most trivial possible way, and hitting an assertion failure.

At this point, if we were using proprietary software (which much of Apple's software is, but luckily libstdc++ is free software), we would have to give up, file a bug report with our vendor, and try to find a workaround. Luckily, this is free software, so we can investigate the root cause. Unfortunately, I don't have the time at the moment to track this down to the root cause, but the source is available for perusal.

You should probably file a bug about this. A workaround in this case would be to remove the _GLIBCXX_DEBUG=1 definition (and probably the _GLIBCXX_DEBUG_PEDANTIC=1 as well). You can do this in Xcode by finding your Target, double clicking on the executable it builds, going to the build tab, making sure the configuration is set to Debug, scrolling down to the GCC 4.2 - Preprocessing section, and deleting the two values from the Preprocessor Macros line. This way the code will build and run, and seems to work in this case, but you'll get fewer assertions that the debug build of the standard library might have been able to catch.

手长情犹 2024-08-23 01:51:42

这看起来是 _GLIBCXX_DEBUG 被 gcc 4.2 破坏的另一种情况Mac OS X

您最好的选择似乎是删除 _GLIBCXX_DEBUG 或切换到 gcc 4.0。

This looks to be another case of _GLIBCXX_DEBUG being broken with gcc 4.2 on Mac OS X.

Your best options look to be to drop _GLIBCXX_DEBUG or to switch to gcc 4.0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文