title: Simple Socket Listener and Threads Memory Leak
body: <p>I have a simple C program that's supposed to listen for connections and start a new thread for each client that connects. The thread simply prints what messages it receives (for now). I followed two tutorials while making this.</p>
  
  <p>It works, but I tried connecting and disconnecting repeatedly with netcat without sending any messages. Each time I connect, the program takes 8KB of memory, but it only releases 4KB when I disconnect. But I can't find the cause of the leak. It ends the thread and closes the socket every time the user disconnects. Here is all of the code involved:</p>
  
  <pre><code>void* clientFunction(void* arg) {
      char receiveBuffer[RECEIVE_BUFFER_SIZE];
      long receiveSize;
      int clntSocket = * ((int*) arg);
  
      while (true) {
          //receive messages
          receiveSize = recv(clntSocket, receiveBuffer, RECEIVE_BUFFER_SIZE, 0);
  
          if (receiveSize &lt;= 0) {
              close(clntSocket);
              return NULL;
          }
  
          printf("Received message: %s", receiveBuffer);
          memset(&amp;receiveBuffer, 0, sizeof(receiveBuffer));
      }
  
      return 0;
  }
  
  int main(int argc, const char * argv[]) {
      //FOR LISTENING SOCKET =====
      int servSock;                    /* Socket descriptor for server */
      int clntSock;                    /* Socket descriptor for client */
      struct sockaddr_in serverAddress; /* Local address */
      struct sockaddr_in clientAddress; /* Client address */
      unsigned int clntLen;            /* Length of client address data structure */
      // =======
  
      /* Create socket for incoming connections */
      if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) &lt; 0) {
          printf("Socket creation failed!\n");
          return SOCKET_ERROR;
      }
  
      memset(&amp;serverAddress, 0, sizeof(serverAddress));   /* Zero out structure */
      serverAddress.sin_family = AF_INET;                /* Internet address family */
      serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
      serverAddress.sin_port = htons(PORT);      /* Local port */
  
      if (bind(servSock, (struct sockaddr *) &amp;serverAddress, sizeof(serverAddress)) &lt; 0) {
          printf("Socket binding failed!\n");
          return SOCKET_ERROR;
      }
  
      if (listen(servSock, MAXPENDING) &lt; 0) {
          printf("Socket listening failed!\n");
          return SOCKET_ERROR;
      }
  
      isListening = true;
      int* arg = &amp;clntSock;
  
      while (isListening) { //should have a timer?
          /* Set the size of the in-out parameter */
          clntLen = sizeof(clientAddress);
  
          /* Wait for a client to connect */
          if ((clntSock = accept(servSock, (struct sockaddr *) &amp;clientAddress, &amp;clntLen)) &gt;= 0) { //??????
              /* clntSock is connected to a client! */
              pthread_t clientThread;
              pthread_create(&amp;clientThread, NULL, &amp;clientFunction, (void*) arg);
          }
      }
  
      return 0;
  }
  </code></pre>