using pthread to create threads, and my application hogs on memory

Discussion in 'C++' started by sharekhan, Feb 3, 2007.

  1. sharekhan

    sharekhan Guest

    Hello,

    I have written a simple relay server. using pthreads. To handle
    multiple clients.
    Design wise its like this.. (my humble apologies if the post is too
    long)

    The thread design was borrowed from this article
    (http://www.codeproject.com/useritems/LikeJavaThreads.asp)

    class IRunnable {
    public:
    virtual void run() = 0;
    };

    class Thread {
    private:
    IRunnable *threadObj;
    pthread_t a_thread;
    void *tresult;

    public:

    Thread(IRunnable *ptr) {
    threadObj = ptr;
    }
    static void *threadProc(void* ptr) {
    ((IRunnable*)ptr)->run();
    return NULL;
    }
    void Thread::start() {
    int result = pthread_create ( &a_thread, NULL, threadProc,
    threadObj);
    printf("Thread START [%s::%d] thread result=%d\n", name, count,
    result);
    }
    void wait() {
    pthread_join(a_thread, 0);
    threadObj = NULL;
    }
    };


    now the server class listens on some port and as soon as someone
    connects hands it over to this new Thread called ClientHandler which
    implements IRunnable.

    server () {
    /*.. Socket definitions etc... */
    while (1) {
    SOCKET clientSocket = accept(mySocket,
    (struct sockaddr *)
    &clientAddr,(int *) &clientAddrLen);
    ClientHandler *ch = new ClientHandler(clientSocket, log);
    Thread *t = new Thread(ch);
    t->start();
    }
    }


    class ClientHandler : public IRunnable
    {
    private:
    //some private data
    SOCKET s;
    sockaddr_in addr;
    char *getbuffer;
    char *sendbuffer;

    char *clientNameCache;
    int oldPort;
    int port;
    int bufSize;
    int result;
    bool disconnectFlag, errorFlag, relent, connected, runflag;
    DataManager *dm;
    PingManager *pm;
    time_t tim;

    public:
    char *myClientId,*hisClientId, *username, *password;

    public:
    //public methods
    ClientHandler(SOCKET &s, ofstream *log);
    ~ClientHandler();
    virtual void run();
    bool touch();
    bool getTime (time_t &tim);
    bool stop();
    };


    Now the issue, is, that the server does not release any memory at the
    end, which means there is a memory leak, the threads are not being
    cleaned up.

    So I wrote another thread.. which recieves a shared list of all the
    threads the server creates.. and all of them in a loop are waiting for
    the client handlers to exit. Now, after the clienthandler exists, and
    I call the destructor, I get an exception..
    the destructor is quite simple..

    ClientHandler::~ClientHandler() {
    try {
    closesocket(s); // stop all comunication
    delete[] getbuffer;
    delete[] sendbuffer; // I get Error Here.. However sendbuffer is
    initialised and used
    delete[] myClientId;
    delete[] hisClientId;
    delete[] clientNameCache;
    delete[] username;
    delete[] password;
    } catch (...) {
    int err = GetLastError();
    *log << "Error In Destroying Client Handler ... " << endl;
    }
    }


    I am sure I am doing something horribly wrong. Any pointers ?
    thanking you all, for reading this awfully newbie code, and making
    sense (if any) of the explanation above.

    Cheers.
    sharekhan, Feb 3, 2007
    #1
    1. Advertising

  2. sharekhan

    Ian Collins Guest

    sharekhan wrote:
    >
    > class Thread {
    > private:
    > IRunnable *threadObj;
    > pthread_t a_thread;
    > void *tresult;
    >
    > public:
    >
    > Thread(IRunnable *ptr) {
    > threadObj = ptr;
    > }
    > static void *threadProc(void* ptr) {
    > ((IRunnable*)ptr)->run();
    > return NULL;
    > }


    Why not pass the Tread object in here, so it can be deleted after run()
    returns, assuming Threads are always created on the heap?

    > void Thread::start() {
    > int result = pthread_create ( &a_thread, NULL, threadProc,
    > threadObj);


    This should at least give a warning as threadProc isn't an extern "C"
    linkage function.

    --
    Ian Collins.
    Ian Collins, Feb 3, 2007
    #2
    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. fran
    Replies:
    2
    Views:
    483
  2. kanchan
    Replies:
    1
    Views:
    811
    Karthik Kumar
    Nov 13, 2004
  3. pthread and multiple CPU

    , Mar 7, 2006, in forum: C Programming
    Replies:
    4
    Views:
    322
    Keith Thompson
    Mar 8, 2006
  4. KDr2
    Replies:
    4
    Views:
    2,474
    Juha Nieminen
    Jun 26, 2007
  5. jakash3

    pthread memory leaks

    jakash3, May 31, 2011, in forum: C++
    Replies:
    15
    Views:
    1,589
    Chris M. Thomasson
    Jun 10, 2011
Loading...

Share This Page