POSIX Threads causing memory leak

H

Heiko Neuhaus

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);
}
}

}
 
H

Heiko Neuhaus

(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.
 
J

Jack Klein

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
 
P

Paul Pluzhnikov

Heiko Neuhaus said:
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,
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top