gpt4 book ai didi

C++ 无法使用 g++-mp-4.4 捕获从 Mac OS 中的 curlpp 抛出的异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:00:39 38 4
gpt4 key购买 nike

我有以下代码(现在可以编译,感谢@LokiAstari):

要编译下面这段代码,最简单的方法是下载curlpp ,编译它。然后用g++-mp-4.4(由macport安装)编译以下代码,链接到curl和curlpp。

如果 localhost:8080 不存在,它将崩溃(输出:abort trap)。将 g++-mp-4.4 更改为 Xcode 4 默认的 g++,它工作正常。

#include <string>
#include <sstream>
#include <iostream>

#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>

int main(int, char **)
{
try
{
curlpp::Cleanup myCleanup;

{
std::ostringstream os;
os << curlpp::options::Url("http://localhost:8080/");
}

}
catch( curlpp::RuntimeError &e )
{
std::cout << e.what() << std::endl;
}

catch( curlpp::LogicError &e )
{
std::cout << e.what() << std::endl;
}

return 0;
}

更新:

在 Easy.cpp 中:

void 
curlpp::Easy::perform()
{
mCurl->perform(); // The exception come from here. mCurl is a std::auto_ptr<internal::CurlHandle> mCurl;
}

......

std::ostream & operator<<(std::ostream & stream, const curlpp::Easy & request)
{
// Quick clone that doesn't copy options, only the curl handle.
curlpp::Easy r(request.getCurlHandle().clone());
r.setOpt(new curlpp::options::WriteStream(& stream));
r.perform(); // The exception come from here.

return stream;
}

在 CurlHandle.cpp 中:

void CurlHandle::perform()
{
CURLcode code;

code = curl_easy_perform(mCurl);
throwException();
libcurlRuntimeAssert(mErrorBuffer, code); //if we got an error
}

在 Exception.cpp 中:

void curlpp::libcurlRuntimeAssert(const std::string & reason, CURLcode code)
{
curlpp::libcurlRuntimeAssert(reason.c_str(), code);
}

void curlpp::libcurlRuntimeAssert(const char * reason, CURLcode code)
{
if (code != CURLE_OK)
throw curlpp::LibcurlRuntimeError(reason, code);
}

我追到了代码,throw curlpp::LibcurlRuntimeError(reason, code);LibcurlRuntimeError的构造函数没问题,然后throw,然后SIGABRT。

堆栈跟踪:

0   __kill      0   0x7fff8213d0b6  
1 abort 0 0x7fff821dd9f6
2 uw_init_context_1 0 0x101663af2
3 _Unwind_Resume 0 0x101663f38
4 operator<< Easy.cpp 118 0x10009b699
5 operator<< Options.cpp 34 0x1000948d8
6 RoleCreationTest::loadPlayerAsyncTestCase rolecreationtest.cpp 147 0x10008c714
7 RoleCreationTest::qt_metacall moc_rolecreationtest.cpp 87 0x1000acab2
8 QMetaMethod::invoke 0 0x1012e6667

最佳答案

各种 MacPorts GCC 4.x 编译器和异常捕获都存在问题,参见示例 GCC bug 42159MacPorts bug 25042 .

关于C++ 无法使用 g++-mp-4.4 捕获从 Mac OS 中的 curlpp 抛出的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7983579/

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