POSIX Threads causing memory leak



 DEVELOP > c-Plus-Plus > POSIX Threads causing memory leak

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Heiko Neuhaus"
Date: 20 Dec 2003 07:48:48 PM
Object: POSIX Threads causing memory leak
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);
}
}
}
.

User: "Paul Pluzhnikov"

Title: Re: POSIX Threads causing memory leak 21 Dec 2003 01:16:06 AM
"Heiko Neuhaus" <hneuhaus@uni-koblenz.de> 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.
.

User: "Heiko Neuhaus"

Title: Re: POSIX Threads causing memory leak 20 Dec 2003 07:51:17 PM
(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.
.

User: "Jack Klein"

Title: Re: POSIX Threads causing memory leak 20 Dec 2003 11:43:28 PM
On Sun, 21 Dec 2003 02:48:48 +0100, "Heiko Neuhaus"
<hneuhaus@uni-koblenz.de> 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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER