By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

POSIX Threads causing memory leak

P: n/a
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);
}
}

}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
(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.
Jul 22 '05 #2

P: n/a
On Sun, 21 Dec 2003 02:48:48 +0100, "Heiko Neuhaus"
<hn******@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
Jul 22 '05 #3

P: n/a
"Heiko Neuhaus" <hn******@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.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.