Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.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
LInux中的C套接字编程,双自由损坏(fasttop)错误_C_Linux_Sockets_Free_Corruption - Fatal编程技术网

LInux中的C套接字编程,双自由损坏(fasttop)错误

LInux中的C套接字编程,双自由损坏(fasttop)错误,c,linux,sockets,free,corruption,C,Linux,Sockets,Free,Corruption,我读过其他类似问题的答案,但没有一个能解决我的问题 这是我的代码: #include <stdlib.h> #include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h>

我读过其他类似问题的答案,但没有一个能解决我的问题

这是我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>

typedef struct {
  pthread_t thread_id;
  int sockfd;
} client_t;

client_t *clients;
size_t client_n = 0;

void *client_thread(void *client_ptr) {
  client_t client = *(client_t*) client_ptr;
  char buffer[500];
  int state;
  while(1) {
    state = send(client.sockfd, 0, 1, MSG_NOSIGNAL);

    if(state == -1) {
      printf("socket-%d closed\n", client.sockfd);
      break;
    }

    read(client.sockfd, buffer, 500);
    printf("from socket-%d: %s\n", client.sockfd, buffer);
    memset(buffer, 0, 500);
  }
  close(client.sockfd);
  free(client_ptr);
  client_n--;
}

int main(int argc, char *argv[]) {
  int sockfd, newsockfd, clilen;
  struct sockaddr_in clientaddr, serveraddr;

  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr = INADDR_ANY;
  serveraddr.sin_port = htons(8080);

  bind(sockfd, (struct sockaddr*) &serveraddr, sizeof(serveraddr));
  listen(sockfd, 5);

  clilen = sizeof(clientaddr);

  clients = (client_t*) malloc(sizeof(client_t));

  while(1) {
    newsockfd = accept(sockfd, (struct sockaddr*) &clientaddr, &clilen);
    printf("New connection: socket-%d\n", newsockfd);
    clients = (client_t*) realloc(clients, (client_n + 1) * sizeof(client_t));
    clients[client_n].sockfd = newsockfd;
    pthread_create(&clients[client_n].thread_id, NULL, client_thread, (void*) &clients[client_n]);
    client_n++;
  }
  return 0;
}
知道问题出在哪里吗?

free(client_ptr);
client_n--;
有两个问题

第一个是
免费
呼叫。您实际上没有为
客户端调用
malloc
(或
realloc
calloc
)。相反,
client_ptr
指向您动态分配的数组,但是
client_ptr
指向的元素本身并不是单独动态分配的。当您传递一个指向
free
的指针时,这会导致未定义的行为,而该指针实际上没有分配给
malloc
和family除了第一个元素(即
客户端[0]
)之外,当您释放整个阵列时。解决这个问题的方法就是不要在线程中调用
free

另一个问题是
客户机--
表达式。您不保护此线程(或
main
函数中相应的
client\u n++
)不被其他线程修改。这意味着两个或多个线程可能会再次同时修改它,从而导致未定义的行为。您需要有一个信号量或互斥来保护此修改



还有几个其他问题。例如,您没有加入已结束的线程,从而导致资源泄漏。您没有检查
read
调用中是否存在错误或关闭的连接(返回
0
read
调用会报告一个关闭良好的连接)。

您的分配已中断。您分配了一个
客户端\u t
数组,但尝试释放一个条目,这是不可能的。只需每次分配一个条目,将其交给客户机线程即可释放它。
free(client_ptr);
client_n--;