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

S

sharekhan

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.
 
I

Ian Collins

sharekhan said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top