POSIX Threads causing memory leak

Discussion in 'C++' started by Heiko Neuhaus, Dec 21, 2003.

  1. Hello all,

    i am Cygwin to create a simple server application. Everything seems to work
    fine but when i use the process manager i can see that my app is leaking
    handles. If i run it under native linux it crashes after seveal minutes -
    which seems to be caused from this error.

    // Dummy
    class tSockInfo
    {
    public:
    int iPort;
    char chIP[4]; int iSocketZ;
    bool bAllForward;

    int iSocket;
    tSockInfo() { bAllForward = false; iSocket = -1; iSocketZ = -1; };
    };

    // Thread-Procedure
    void* dummyThreadProc (void* pArgs)
    {
    tSockInfo* sockinfo = (tSockInfo*)pArgs;
    int q;

    close (sockinfo->iSocket);
    delete ((tSockInfo*)pArgs);

    pthread_exit (NULL);
    }

    // Main-Routine
    int main()
    {
    int iSocket = socket (AF_INET, SOCK_STREAM, 0);

    sockaddr_in saddr = {0};

    saddr.sin_family = AF_INET;
    saddr.sin_port = htons (socksport);
    saddr.sin_addr.s_addr = htonl (INADDR_ANY);

    if (bind (iSocket, (sockaddr*)(&saddr), sizeof (saddr)))
    {
    die ("Unable to bind socket to port. Exiting.");
    }

    if (listen (iSocket, 5))
    {
    die ("Unable to listen on port. Exiting.");
    }

    while (1)
    {
    int iLen = 0;
    sockaddr asin;
    int ASock = accept (iSocket, (sockaddr*)&asin, &iLen);

    if (ASock < 0)
    {
    die ("Accept returned invalid socket. Exiting.");
    }
    else
    {
    tSockInfo* sock = new tSockInfo;
    sock->iSocket = ASock;
    pthread_t iThreadID;
    pthread_create (&iThreadID, NULL, dummyThreadProc, sock);
    }
    }

    }
    Heiko Neuhaus, Dec 21, 2003
    #1
    1. Advertising

  2. (sorry for additional posting)

    The problem must be located within those 4 lines.

    tSockInfo* sock = new tSockInfo;
    sock->iSocket = ASock;
    pthread_t iThreadID;
    pthread_create (&iThreadID, NULL, dummyThreadProc, sock);


    Any help would be greatly appreciated.

    Thanks alot in advance.
    Heiko Neuhaus, Dec 21, 2003
    #2
    1. Advertising

  3. Heiko Neuhaus

    Jack Klein Guest

    On Sun, 21 Dec 2003 02:48:48 +0100, "Heiko Neuhaus"
    <> wrote in comp.lang.c++:

    > Hello all,
    >
    > i am Cygwin to create a simple server application. Everything seems to work
    > fine but when i use the process manager i can see that my app is leaking
    > handles. If i run it under native linux it crashes after seveal minutes -
    > which seems to be caused from this error.


    [snip]

    Please leave comp.lang.c++ off your cross-post list for questions like
    this in the future. The C++ language does not define or support
    threads, processes, or handles. These are all platform specific
    extensions, and this is not a language issue.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Dec 21, 2003
    #3
  4. "Heiko Neuhaus" <> writes:

    > while (1) {

    ....
    > int ASock = accept (iSocket, (sockaddr*)&asin, &iLen);

    ....
    > tSockInfo* sock = new tSockInfo;
    > sock->iSocket = ASock;
    > pthread_t iThreadID;
    > pthread_create (&iThreadID, NULL, dummyThreadProc, sock);
    > }
    > }


    You must create your threads detached, or you must pthread_join them.

    Also note that you are using a "one thread per client" pattern,
    which is discouraged (it is acceptable for a homework exercise,
    but it is not a good way to write production code).

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.
    Paul Pluzhnikov, Dec 21, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SWxhbiBPc3Npbg==?=

    .Net Seems to be Causing DLL Memory Leak?

    =?Utf-8?B?SWxhbiBPc3Npbg==?=, Feb 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    540
    Amit Bahree
    Mar 9, 2005
  2. Evan David Light
    Replies:
    6
    Views:
    463
    David Anderson
    Dec 1, 2003
  3. Chaman Singh

    STL map and Posix Threads

    Chaman Singh, Apr 8, 2004, in forum: C++
    Replies:
    3
    Views:
    539
    Nick Hounsome
    Apr 9, 2004
  4. Christian Buckl
    Replies:
    6
    Views:
    688
    Alf P. Steinbach
    Oct 8, 2004
  5. JackC
    Replies:
    14
    Views:
    638
    James Kanze
    Oct 16, 2007
Loading...

Share This Page