Linux下AWS C++ SDK调用PutObject触发std::bad_alloc错误求助
Linux下AWS C++ SDK S3操作触发std::bad_alloc问题排查
问题背景
在Linux环境使用AWS C++ SDK共享库向S3传输文件时触发std::bad_alloc错误,尝试运行AWS官方S3列表示例代码后仍出现相同错误,错误发生在调用AwsDoc::S3::PutObject(bucket_name, object_name, clientConfig);时。
编译命令:
g++ testingAws.cpp -I . -L . -laws-cpp-sdk-transfer -laws-cpp-sdk-core -laws-cpp-sdk-s3
运行报错:
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
GDB堆栈回溯信息:
Backtrace: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Thread 1 "a.out" received signal SIGABRT, Aborted. __pthread_kill_implementation (no_tid=0, signo=6, threadid=140737314626112) at ./nptl/pthread_kill.c:44 44 ./nptl/pthread_kill.c: No such file or directory. (gdb) bt #0 __pthread_kill_implementation (no_tid=0, signo=6, threadid=140737314626112) at ./nptl/pthread_kill.c:44 #1 __pthread_kill_internal (signo=6, threadid=140737314626112) at ./nptl/pthread_kill.c:78 #2 __GI___pthread_kill (threadid=140737314626112, signo=signo@entry=6) at ./nptl/pthread_kill.c:89 #3 0x00007ffff6b2d476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26 #4 0x00007ffff6b137f3 in __GI_abort () at ./stdlib/abort.c:79 #5 0x00007ffff6dd5bbe in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6 #6 0x00007ffff6de124c in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6 #7 0x00007ffff6de12b7 in std::terminate() () from /lib/x86_64-linux-gnu/libstdc++.so.6 #8 0x00007ffff6de1518 in __cxa_throw () from /lib/x86_64-linux-gnu/libstdc++.so.6 #9 0x00007ffff6dd57cc in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6 #10 0x00007ffff6e7ee6f in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /lib/x86_64-linux-gnu/libstdc++.so.6 #11 0x00007ffff6e7f22d in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /lib/x86_64-linux-gnu/libstdc++.so.6 #12 0x000055555555b58d in Aws::S3::Model::PutObjectRequest::SetBucket(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () #13 0x000055555555a78b in AwsDoc::S3::PutObject(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Aws::Client::ClientConfiguration const&) () #14 0x0000000000000000 in ?? () (gdb)
可能的遗漏配置及调试方案
1. 强制C++标准版本一致性
从堆栈回溯看,错误发生在std::string赋值操作,大概率是C++标准版本不匹配导致的ABI兼容问题:
- 确认编译AWS SDK时使用的C标准(如C11/C++17),编译代码时必须指定相同标准,例如:
g++ testingAws.cpp -std=c++11 -I . -L . -laws-cpp-sdk-transfer -laws-cpp-sdk-core -laws-cpp-sdk-s3 - 确保SDK和你的代码使用相同的标准库(均为libstdc或libc),避免混合使用。
2. 修复共享库加载路径问题
编译时指定-L .仅告知编译期库位置,运行时系统可能找不到这些库,间接触发内存异常:
- 运行前设置环境变量指定库路径:
export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH - 或编译时添加
-Wl,-rpath=./将库路径嵌入可执行文件:g++ testingAws.cpp -std=c++11 -I . -L . -Wl,-rpath=./ -laws-cpp-sdk-transfer -laws-cpp-sdk-core -laws-cpp-sdk-s3
3. 补全AWS客户端配置
确保clientConfig初始化完整,至少设置存储桶所在区域:
Aws::Client::ClientConfiguration clientConfig; clientConfig.region = "us-east-1"; // 替换为你的S3存储桶区域
未配置区域会导致SDK内部初始化失败,进而触发内存分配错误。
4. 深入调试错误根源
- 开启SDK日志:在代码开头添加日志配置,查看SDK内部错误细节:
Aws::SDKOptions apiOptions; Aws::InitAPI(apiOptions); Aws::Utils::Logging::ConsoleLogSystem::Initialize(Aws::Utils::Logging::LogLevel::Debug); - GDB断点调试:在
PutObject处打断点,检查传入的字符串参数是否有效(如空指针、已释放内存):break AwsDoc::S3::PutObject run print bucket_name print object_name - 检查系统内存:用
free -h确认系统内存是否充足,内存耗尽也可能触发std::bad_alloc。
5. 重新编译AWS SDK确保完整性
重新编译SDK时,明确指定编译共享库:
cmake -DBUILD_SHARED_LIBS=ON .. make -j$(nproc)
编译过程中检查是否有警告或错误,确保curl、openssl等依赖库已正确安装。
内容的提问来源于stack exchange,提问作者Abhishekh
相关产品推荐
相关产品推荐

