gpt4 book ai didi

c++ - 全局 std::string 在 iOS 上导致崩溃

转载 作者:可可西里 更新时间:2023-11-01 03:58:11 24 4
gpt4 key购买 nike

我已将此作为错误提交给 Apple,但为了确认,这里是测试代码:

#include <string>

std::string home_directory;

std::string BuildPath(const std::string directory, const std::string path)
{
if(home_directory.compare(directory) == 0)
printf("In home directory\n");

return directory + "/" + path;
}

int main(int, char* [])
{
home_directory = "home";
printf("Home: '%s'\n", home_directory.c_str());
printf("BuildPath: '%s'\n", BuildPath("base", "path").c_str());
}

当使用最新的 XCode 5.1、iOS SDK 7.1 和 LLVM 5.1 构建时,将 libstdc++ 用于 C++ 标准库,当在 iOS 5.1 上运行时,这会在 BuildPath 函数的返回行上的 std::string 实现中的某处崩溃设备。

输出是

Home: 'home'
CrashTest(1242) malloc: *** error for object 0x2fe2ac80: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

堆栈爬取:

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x00000000, 0x00000000
Crashed Thread: 0

Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x34fb8848 __kill + 8
1 libsystem_c.dylib 0x36eae2ae abort + 110
2 libsystem_c.dylib 0x36e6937a free + 374
3 libstdc++.6.dylib 0x3481a93a operator delete(void*) + 6
4 libstdc++.6.dylib 0x34806138 std::string::_Rep::_M_dispose(std::allocator<char> const&) + 68
5 libstdc++.6.dylib 0x34806c04 std::string::reserve(unsigned long) + 156
6 libstdc++.6.dylib 0x34806daa std::string::append(char const*, unsigned long) + 70
7 CrashTest 0x00094a30 BuildPath(std::string, std::string) (basic_string.h:2121)
8 CrashTest 0x00094bda main (main.cpp:25)
9 CrashTest 0x0009499c start + 32

使用 -O1 或更低的优化级别,或使用 libc++ 作为标准库,它可以按预期工作。它也可以在 iOS 6 或 7 上按预期工作。当使用以前版本的 XCode(5.0.2、iOS SDK 7.0 和 LLVM 5.0)构建时,无论优化设置如何,它都可以正常工作。

注释掉与全局字符串的比较也可以避免崩溃。

任何人都可以看到我的代码有什么问题吗?如果不是,关于坠机原因的任何理论?也许一个新的 LLVM 优化会触发 iOS 5.1 中 libstdc++ 运行时的错误?

我能想到的另一个选项是优化器正在生成无效代码。那会更让人担心。

最佳答案

Apple 为 Xcode 5.1.1 发布了一个新的 GM 种子。在发行说明中,他们说他们修复了几个崩溃问题:

Fixed a compiled code crash on when targeting iOS 5.1.1. (16485980)!

Fixed a compiled code crash when using ARC and C++. (16368824)

http://adcdownload.apple.com//Developer_Tools/xcode_5.1.1_gm_seed/release_notes_xcode_5.1.1_gm_seed.pdf

关于c++ - 全局 std::string 在 iOS 上导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22662420/

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