public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Peter Rosin <peda@lysator.liu.se>
To: cygwin@cygwin.com
Subject: Re: Memory leak in select
Date: Mon, 18 Apr 2011 15:03:00 -0000	[thread overview]
Message-ID: <4DAC2D35.5070106@lysator.liu.se> (raw)
In-Reply-To: <4DAC23E3.2020005@lysator.liu.se>

Den 2011-04-18 13:43 skrev Peter Rosin:
> Hi!
> 
> Using the following STC, I'm seeing what appears to be a memory
> leak in select(2).
> 
> ----------------8<---(selectleak.c)---------
> #include <sys/time.h>
> #include <fcntl.h>
> #include <stdlib.h>
> 
> int
> main(void)
> {
> 	fd_set fdset;
> 	struct timeval tv;
> 
> 	long flags = fcntl(0, F_GETFL);
> 	fcntl(0, F_SETFL, flags | O_NONBLOCK);
> 
> 	for (;;) {
> 		int res;
> 		char buf[20];
> 
> 		FD_ZERO(&fdset);
> 		FD_SET(0, &fdset);
> 		tv.tv_sec = 1;
> 		tv.tv_usec = 0;
> 		res = select(1, &fdset, NULL, NULL, &tv);
> 		if (res < 0)
> 			exit(1);
> 		if (!res)
> 			continue;
> 		res = read(0, buf, sizeof(buf));
> 		if (res < 0)
> 			exit(1);
> 	}
> 
> 	return 0;
> }
> ----------------8<--------------------------
> 
> $ gcc -o selectleak selectleak.c
> $ cat /dev/zero | ./selectleak
> 
> Note that "./selectleak < /dev/zero" does not trigger the leak for me (I haven't
> seen the memory usage go up, so at least not as reliably).
> 
> An "strace -m malloc,select" has this:
> 
>   156   59281 [main] selectleak 8440 cygwin_select: 1, 0x23CCFC, 0x0, 0x0, 0x0
>    64   59345 [main] selectleak 8440 calloc: (1, 44) = 49AF58, called by 61002389
>    63   59408 [main] selectleak 8440 calloc: (1, 12) = 49B018, called by 61002389
>   111   59519 [main] selectleak 8440 cygwin_select: to NULL, ms FFFFFFFF
>    62   59581 [main] selectleak 8440 cygwin_select: sel.always_ready 0
>   138   59719 [select_pipe] selectleak 8440 peek_pipe: , ready for read: avail 130892
>   160   59879 [main] selectleak 8440 select_stuff::wait: woke up.  wait_ret 1.  verifying
>    57   59936 [main] selectleak 8440 set_bits: me 0x49AF58, testing fd 0 ()
>    62   59998 [main] selectleak 8440 set_bits: ready 1
>    62   60060 [main] selectleak 8440 select_stuff::wait: gotone 1
>    61   60121 [main] selectleak 8440 select_stuff::wait: returning 0
>    61   60182 [main] selectleak 8440 select_stuff::cleanup: calling cleanup routines
>   104   60286 [main] selectleak 8440 free: (0x49B018), called by 0x610BC1EA
>    65   60351 [main] selectleak 8440 peek_pipe: , already ready for read
>    62   60413 [main] selectleak 8440 set_bits: me 0x49AF58, testing fd 0 ()
>    62   60475 [main] selectleak 8440 set_bits: ready 1
>    74   60549 [main] selectleak 8440 select_stuff::poll: returning 1
>    61   60610 [main] selectleak 8440 select_stuff::cleanup: calling cleanup routines
>    65   60675 [main] selectleak 8440 select_stuff::~select_stuff: deleting select records
>    60   60735 [main] selectleak 8440 free: (0x49AF58), called by 0x610BC2A2
>   108   60843 [main] selectleak 8440 calloc: (1, 44) = 49AF58, called by 61002389
>    64   60907 [main] selectleak 8440 calloc: (1, 12) = 49B018, called by 61002389
>   126   61033 [main] selectleak 8440 peek_pipe: , ready for read: avail 130892
>    62   61095 [main] selectleak 8440 fhandler_base::ready_for_read: read_ready 1, avail 1
>    62   61157 [main] selectleak 8440 select_stuff::cleanup: calling cleanup routines
>    61   61218 [main] selectleak 8440 select_stuff::cleanup: calling cleanup routines
>    62   61280 [main] selectleak 8440 select_stuff::~select_stuff: deleting select records
>    61   61341 [main] selectleak 8440 free: (0x49AF58), called by 0x610BC2A2
>   226   61567 [main] selectleak 8440 cygwin_select: 1, 0x23CCFC, 0x0, 0x0, 0x0
> 
> Notice how the "calloc: (1, 12) = 49B018" is only freed once between the two
> "cygwin_select: 1, 0x23CCFC, ..."-lines.
> 
> $ cygcheck -c cygwin gcc4
> Cygwin Package Information
> Package              Version        Status
> cygwin               1.7.9-1        OK
> gcc4                 4.3.4-4        OK
> 
> Cheers,
> Peter

Arrrg, this is the program I actually used to generate that
strace (but the leak is present in both versions). Oh well,
sorry about that...

----------------8<---(selectleak.c)---------
#include <sys/time.h>
#include <fcntl.h>

int
main(void)
{
	fd_set fdset;

	long flags = fcntl(0, F_GETFL);
	fcntl(0, F_SETFL, flags | O_NONBLOCK);

	for (;;) {
		int res;
		char buf[20];

		FD_ZERO(&fdset);
		FD_SET(0, &fdset);
		res = select(1, &fdset, NULL, NULL, NULL);
		if (!res)
			continue;
		if (res < 0)
			return 1;
		res = read(0, buf, sizeof(buf));
		if (!res)
			break;
		if (res < 0)
			return 1;
	}

	return 0;
}
----------------8<--------------------------

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

  reply	other threads:[~2011-04-18 12:23 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-18 13:57 Peter Rosin
2011-04-18 15:03 ` Peter Rosin [this message]
2011-04-18 15:11   ` Peter Rosin
2011-04-18 15:40     ` Christopher Faylor
2011-04-18 15:54       ` Christopher Faylor
2011-04-18 19:35         ` Peter Rosin
2011-04-19 23:30           ` Peter Rosin
2011-04-20  5:16             ` Christopher Faylor
2011-04-20 12:08               ` Peter Rosin
2011-04-20 13:49               ` Thomas Stalder
2011-04-20 13:59                 ` Corinna Vinschen
2011-04-20 15:23                   ` Thomas Stalder
2011-04-19  7:53       ` Peter Rosin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4DAC2D35.5070106@lysator.liu.se \
    --to=peda@lysator.liu.se \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).