LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-04-2008, 01:09 AM   #1
greeklegend
Member
 
Registered: Feb 2006
Location: At a computer
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75

Rep: Reputation: 15
C pthreads memory leak


...or rather memory floods!
My (server) application has an infinite loop with a blocking accept(), and when accept() returns does a pthread_create() to spawn a thread to deal with the new connection.
However, for some reason each time I accept a connection the memory usage of my program goes up by 8 MB!!! Eventually, pthread_create() fails and sets errno to 12, which i think is ENOMEM.
However, when i don't do it with threads, there is no memory leak at all. To demonstrate-
Code:
while (1)
{
curreq = accept(localsock, (struct sockaddr*) &cli_addr, &clilen);
#ifdef __THREAD
rval = pthread_create(&thread, NULL, (void*)tunnel_thread, curreq);
if (rval != 0){
fprintf(stderr, "pthread_create() failed with %i", errno);
exit(1);}
#endif
#ifndef __THREAD
tunnel_thread(curreq);
#endif
}
And tunnel_thread looks like this
Code:
void tunnel_thread(int curreq)
{
//do work
close(curreq);
pthread_exit(NULL);
return;
}
Now, if __THREAD is defined, my program leaks worse than a cane basket, consuming up to 2gb of virtual memory and services about 30 connections before pthread_create() starts failing.
If __THREAD is undefined, my program works perfectly (apart from the fact that i can only service one connection at a time) and doesn't leak.
What am I doing wrong? I'll post more code if you need it, just thought i'd keep the problem general and simple.
 
Old 01-04-2008, 01:31 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Try pthread_attr_setstacksize (()?

Have you tried tuning the default thread stack size:

http://www.cygwin.com/ml/libc-alpha/.../msg00002.html

http://ahlamnote.blogspot.com/2007/0...f-threads.html
 
Old 01-04-2008, 05:57 AM   #3
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
this is related to the stack size growing but not shrinking. Unfortunately, I don't know of any real way of actually freeing this memory. Google might be of some assistance.

Last edited by orgcandman; 01-04-2008 at 06:05 AM.
 
Old 01-05-2008, 07:13 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
It is a long time ago I programmed threads, so let me try:

If the thread is not detached, it will retain all memory until it is joined. I am not sure you are creating the thread with the detached option. Check this document: SUN threaded programming guide

Check anyway of you are releasing everything and cleaning up everything in the correct way. One error in exiting or terminating the thread never releases the allocated memory.

In the glibc reference guide there is a chapter which explains a multi-threaded server, exactly waht you do here. Did you compare code?

Last but not least, some weeks ago there was thread on this forum dealing with memory leaks in threads, which was clearly a bug. But I cannot find it so quickly, you might try to search yourself.

jlinkels
 
Old 01-07-2008, 04:18 AM   #5
greeklegend
Member
 
Registered: Feb 2006
Location: At a computer
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75

Original Poster
Rep: Reputation: 15
Ah, pthread_detach() seems to solve the problem
Thanks you guys.
On with the process of debugging...and what a horrendus list of bugs it is
In fact i don't think there's a single thing that DOES work properly....ah well back to it :P
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leak? habl Slackware 11 02-02-2007 03:44 PM
memory leak ? os2 Programming 1 05-19-2005 01:45 PM
Memory Leak?! RoaCh Of DisCor Linux - General 9 05-07-2005 12:26 AM
what is memory leak blackzone Programming 6 11-05-2004 10:32 AM
Memory Leak when using memory debugging C program on SuSE SLES8 babalina Linux - Distributions 0 10-06-2003 09:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration