basic_string析构函数中的段错误

segfault in basic_string destructor

本文关键字:错误 段错误 string 析构函数 basic      更新时间:2023-10-16

我有一个函数ModuleManager::tick(),代码如下:

void ModuleManager::tick()
{
auto now = std::chrono::steady_clock::now();
auto nowSys = std::chrono::system_clock::now().time_since_epoch();
for(auto& m : m_modules)
{
    if(std::chrono::duration_cast<std::chrono::seconds>(now - m.second) >=
        std::chrono::seconds(m.first.m_settings.m_interval))
    {
        std::string result = m.first.run(); //run() returns a std::string
        m.second = now;
        try
        {
            HTTPConn conn("127.0.0.1", 80);
            conn.request("POST", "/", std::vector<std::string>{"Host: localhost", "Connection: close"}, result);
        }
        catch(HTTPException& e)
        {
            Log::write(e.getErrorString());
        }
    }
}

程序在从HTTPConn::request()函数返回时,在basic_string析构函数中出现分段错误(已经使用GDB确定了这一点)。如果我注释掉request()函数中的所有代码,仍然会出现段错误,所以问题一定在该函数之外。

我相信问题是,在我的HTTPConn构造器的某个地方,我破坏了堆。代码如下:

HTTPConn::HTTPConn(const std::string& host, int port)
{
addrinfo hints;
addrinfo* res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int result = getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, &res);
if(result)
{
    throw HTTPException(HTTPE_GETADDRINFO_FAILED);
}
addrinfo* ptr = res;
bool validSocket = false;
while(ptr)
{
    m_socket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
    if(m_socket == -1)
    {
        ptr = ptr->ai_next;
    }
    else
    {
        validSocket = true;
        break;
    }
}
if(!validSocket)
{
    freeaddrinfo(res);
    throw HTTPException(HTTPE_SOCKET_FAILED);
}
result = connect(m_socket, ptr->ai_addr, ptr->ai_addrlen);
freeaddrinfo(res);
if(result == -1)
{
    close(m_socket);
    m_socket = -1;
    if(errno == ECONNREFUSED)
    {
        throw HTTPException(HTTPE_CONNECTION_REFUSED);
    }
    else if(errno == ENETUNREACH)
    {
        throw HTTPException(HTTPE_NETWORK_UNREACHABLE);
    }
    else if(errno == ETIMEDOUT)
    {
        throw HTTPException(HTTPE_TIMED_OUT);
    }
    else
    {
        throw HTTPException(HTTPE_CONNECT_FAILED);
    }
}
}

我为大量的代码道歉;我试图做一个简短的、独立的例子,但无法重现这个问题。

<标题> 更新

所以问题似乎是我没有在HTTPConn::请求函数中返回任何std::string对象,但它被声明为具有std::string返回类型。我现在的问题是:为什么这是编译?这是我用来编译它的命令行,使用g++ 4.8.2:

g++ -Iinclude -std=c++11 -g -D__DEBUG -c src/HTTP.cpp -o obj/HTTP.o

没有警告或错误。

问题是我用返回类型std::string声明了HTTPConn::request()函数,但是没有返回任何东西。正如fracry Hamidi所说,这会导致未定义的行为。

在我看来,这应该是一个在g++中默认启用的警告,因为它会导致未定义的行为。或者这应该是一个错误。将-Wall标志添加到编译命令中会启用此警告(或者-Wreturn-type只启用特定的警告)