Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 基本字符串析构函数中的segfault_C++_Segmentation Fault_G++ - Fatal编程技术网

C++ 基本字符串析构函数中的segfault

C++ 基本字符串析构函数中的segfault,c++,segmentation-fault,g++,C++,Segmentation Fault,G++,我有一个函数,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::

我有一个函数,
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::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::request函数中返回任何
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()
函数,但没有返回任何内容。正如弗里德里克·哈米迪所说,这会导致未定义的行为


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

我曾经遇到过类似的问题,这是由使用一个版本的Visual Studio(和运行库)编译DLL引起的,但使用另一个版本的Visual Studio编译可执行文件(和运行库)。它们链接良好,但任何涉及传递
std::vector
std::string
等的DLL调用都会以某种方式失败,而这种方式根据语言是没有意义的。我建议先看一看:确保项目中的所有内容都是由相同的编译器、该编译器的相同版本以及基本上相同的编译器标志。我在构造函数中没有看到任何可能直接损坏内存的地方。我看到的唯一问题是,在调用
close
之前,您应该保存
errno
,因为
close
可能会设置
errno
。请显示
请求
函数。那么问题可能就不存在了你认为它在哪里?至少你可以给我们看一下
请求的声明
?@JoachimPileborg谢谢,当我去复制声明时,我注意到std::string函数上有一个返回类型,但是我没有返回std::string。请看我问题中的更新。@Brett,如果你把
-Wall
传递给
g++
,它应该警告函数缺少返回值。但是,默认情况下不会生成错误,因为标准仅认为它是未定义的行为。