Memory leak in select

Peter Rosin peda@lysator.liu.se
Tue Apr 19 23:30:00 GMT 2011


Den 2011-04-18 21:23 skrev Peter Rosin:
> Den 2011-04-18 17:28 skrev Christopher Faylor:
>> On Mon, Apr 18, 2011 at 11:24:41AM -0400, Christopher Faylor wrote:
>>> On Mon, Apr 18, 2011 at 04:32:10PM +0200, Peter Rosin wrote:
>>>> Den 2011-04-18 14:23 skrev Peter Rosin:
>>>>> 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>
>>>>>
>>>>> 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<--------------------------
>>>>
>>>> Ok, I'm taking a wild swing at this, and my guess is that the call
>>>> sel.cleanup () in cygwin_select prematurely zeros out the cleanup
>>>> member of the select_record. The call to sel.poll () then adds
>>>> "stuff" to the select_record that really should have been cleaned
>>>> up, but isn't since cleanup has already been executed and then
>>>> zapped (by select_stuff::cleanup).
>>>>
>>>> But what do I know?
>>>
>>> How does sel.poll add "stuff" that should be cleaned up?  That function
>>> only looks for bits to set.
> 
> I shouldn't have included the strace, and I shouldn't have guessed about
> the cause of the problem without verifying my claims. Sorry about that.
> But for the record the included strace snippet is reoccurring like that
> many many times (the address of the allocation that isn't freed is
> moving).  Further evidence; the STC leaks 1 MB every 3 seconds on my
> computer, that just can't be right.

Back with a patch this time.  Fixes it for me...

Cheers,
Peter

2011-04-19  Peter Rosin  <peda@lysator.liu.se>

	* select.cc (pipe_cleanup): Don't leak a select_pipe_info when a
	thread turned out not to be needed.

Index: select.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/select.cc,v
retrieving revision 1.163
diff -u -r1.163 select.cc
--- select.cc	4 Apr 2011 12:23:36 -0000	1.163
+++ select.cc	19 Apr 2011 21:25:44 -0000
@@ -644,13 +644,15 @@
 pipe_cleanup (select_record *, select_stuff *stuff)
 {
   select_pipe_info *pi = (select_pipe_info *) stuff->device_specific_pipe;
-  if (pi && pi->thread)
+  if (!pi)
+    return;
+  if (pi->thread)
     {
       pi->stop_thread = true;
       pi->thread->detach ();
-      delete pi;
-      stuff->device_specific_pipe = NULL;
     }
+  delete pi;
+  stuff->device_specific_pipe = NULL;
 }
 
 int

--
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



More information about the Cygwin mailing list