Thread: Leaking memory when creating threads

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    Leaking memory when creating threads

    I have a windows service that updates a software package in the background. the main program communicates with this service by means of a named pipe. the buffer size for the pipe is 4096 bytes. when I spawn a new thread for each pipe connection, it appears to leak memory in approximately the amount of the pipe buffer size for each thread I create. I can handle connections without spawning threads, because the connection handles one request and then closes, and requests are responded to very quickly.

    The main thread spawning loop:
    Code:
    #define BUFSIZE 4096
    
      while (true)
      {
        HANDLE hPipe = CreateNamedPipe(lpszPipename,             // pipe name
                                       PIPE_ACCESS_DUPLEX,       // read/write access
                                       PIPE_TYPE_BYTE |       // message type pipe
                                       PIPE_READMODE_BYTE |   // message-read mode
                                       PIPE_WAIT,                // blocking mode
                                       PIPE_UNLIMITED_INSTANCES, // max. instances
                                       BUFSIZE,                  // output buffer size
                                       BUFSIZE,                  // input buffer size
                                       0,                        // client time-out
                                       NULL);                    // default security attribute
    
        if (hPipe == INVALID_HANDLE_VALUE)
        {
          return 0xFFFFFFFF;
        }
    
        fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
    
        if (fConnected)
        {
          HANDLE hThread = NULL;
          unsigned int threadid = 0;
    
          ThreadUpdaterStatus* tus = new ThreadUpdaterStatus;
          tus->hPipe = hPipe;
          tus->upd = (UpdaterStatus*)data;
          hThread = (HANDLE)_beginthreadex(NULL, 0x10000, (THREADPROC)PipeClientThread, (void*)tus, 0, &threadid);
          if (!hThread) return 0xFFFFFFFF;
          CloseHandle(hThread);
        }
      }
    The thread function:
    Code:
    unsigned int WINAPI PipeClientThread(volatile ThreadUpdaterStatus* data)
    {
      HANDLE hPipe = data->hPipe;
      UpdaterStatus* upd = data->upd;
    
      // Read data from pipe
      ReadFile(hPipe...);
    
      // Process Request
    
      // Write data to pipe
      WriteFile(hPipe...);
    
      FlushFileBuffers(hPipe);
      DisconnectNamedPipe(hPipe);
      CloseHandle(hPipe);
      _endthreadex(0);
      return 0;
    }
    My question is simply this: is there anything else I need to do with the pipe or the thread when the thread terminates? the tutorials I read on pipes and threads don't mention anything else, but in my experience, tutorials give you enough to get it working, and sometimes skimp on the real details. This service cannot be allowed to eat up 4k every second until the machine comes crashing down.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are only closing the pipe handle if it was connected successfully. You should always close the pipe if the call to CreateNamedPipe() succeeded.

    The ThreadUpdaterStatus that you pass to the thread is never deleted. You need to delete that in the thread procedure.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by bithub View Post
    You are only closing the pipe handle if it was connected successfully. You should always close the pipe if the call to CreateNamedPipe() succeeded.
    I'll give that a try and see what happens.

    The ThreadUpdaterStatus that you pass to the thread is never deleted. You need to delete that in the thread procedure.
    as I'm sure you can tell from the code, the example I gave has much stripped out of it, including the delete of the ThreadUpdaterStatus object. I simply forgot to include it in my post. Good catch though.

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    The deletion and handle closing is also forgone if thread creation fails (nothing looks to have been stripped out there).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  2. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  3. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  4. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  5. Creating threads from a class
    By Niara in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2006, 03:52 PM