gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:19:35 26 4
gpt4 key购买 nike

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

#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) , 比没有内存泄漏

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

最佳答案

当您调用 std::exit 时从信号处理程序中,堆栈没有展开,局部变量如 std::string对象 tmpmain没有被破坏。

tmp在程序终止之前没有被销毁,它分配的内存永远不会被释放,因此发生泄漏。

关于SIGINT 上的 C++ 程序内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5924603/

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