SIGINT上的C ++程序内存泄漏

时间:2011-05-07 23:10:09

标签: c++ c memory-leaks

我有一个程序,用作套接字客户端,这里是代码

#include <stdio.h>
#include <signal.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <netdb.h>

using namespace std;

void signalHandler(const int signal) {
    cout << "SIGINT handled" << endl;
    exit(EXIT_SUCCESS);
}

void error(const char *msg) {
    perror(msg);
    exit(0);
}

const string readStr(int descriptor) {
    int n = 0;
    string cmd = "";

    char buffer[256];
    memset(buffer, 0, sizeof(buffer));

    while ((n = read(descriptor, buffer, 255)) != 0) {
        if (n < 0) {
            error("Error reading string");
        }

        // full string - just copy
        if (n == 255) {
            cmd += buffer;
            memset(buffer, 0, sizeof(buffer));
        }
        else {
            cmd += buffer;
            break;
        }
    }

    return cmd;
}

int main(int argc, char** argv) {
    signal(SIGINT, signalHandler);

    int fd, port = (argc >= 3 ? atoi(argv[2]) : 11212), n;
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0) {
        error("ERROR opening socket");
    }

    struct hostent *server = (argc >= 2 ? gethostbyname(argv[1]) : gethostbyname("localhost"));
    if (server == NULL) {
        error("ERROR no such host");
    }

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));

    addr.sin_family = AF_INET;
    bcopy(server->h_addr, &addr.sin_addr.s_addr, server->h_length);
    addr.sin_port = htons(port);

    if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        error("ERROR connecting");
    }

    string tmp;
    while (1) {
        cout << "Please enter the message: \n< " << flush;
        tmp = readStr(0);
        n = send(fd, tmp.c_str(), tmp.size(), 0);
        if (n < 0) {
            perror("ERROR writing to socket");
        }

        tmp = readStr(fd);
        cout << "> " << tmp << endl;
    }
    close(fd);

    return EXIT_SUCCESS;
}

当我发送SIGNINT进程(CTRL + C)时,valgrind说:

^CSIGINT handled
==3798==
==3798== HEAP SUMMARY:
==3798==     in use at exit: 39 bytes in 1 blocks
==3798==   total heap usage: 45 allocs, 44 frees, 4,778 bytes allocated
==3798==
==3798== 39 bytes in 1 blocks are possibly lost in loss record 1 of 1
==3798==    at 0x402471C: operator new(unsigned int)
(vg_replace_malloc.c:255)
==3798==    by 0x40DBB64: std::string::_Rep::_S_create(unsigned int,
unsigned int, std::allocator const&) (in
/usr/lib/libstdc++.so.6.0.15)
==3798==    by 0x1BFF403: ???
==3798==
==3798== LEAK SUMMARY:
==3798==    definitely lost: 0 bytes in 0 blocks
==3798==    indirectly lost: 0 bytes in 0 blocks
==3798==      possibly lost: 39 bytes in 1 blocks
==3798==    still reachable: 0 bytes in 0 blocks
==3798==         suppressed: 0 bytes in 0 blocks
==3798==
==3798== For counts of detected and suppressed errors, rerun with: -v
==3798== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 20 from 9)

如果程序正确退出,例如,如果while(1)重播int i = 0; while(i++ <= 2),则没有内存泄漏

那么有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

当您从信号处理程序调用{​​{1}}时,堆栈未展开,并且std::exit中的std::string对象tmp等局部变量不会被销毁。

由于main在程序终止之前没有被销毁,所以它分配的内存永远不会被释放,因此泄漏。