X-Spam-Check-By: sourceware.org Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Subject: _kbhit & memory leak in select() Date: Sun, 5 Feb 2006 13:17:33 -0500 Message-ID: <6CA15ADD82E5724F88CB53D50E61C9AE9ECE45@cgcmail.cgc.cpmc.columbia.edu> From: "Michiel De Hoon" To: Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id k15IHgeX032139 Hi everybody, For one of my software projects, I need the _kbhit function to check the console for keyboard input. While this function is present in msvcrt.dll, it is missing from cygwin1.dll, so I started writing this function myself (I'm hoping to contribute it to Cygwin if it works well). However, when writing this function it appeared that there is a memory leak in Cygwin's select() function. To get a _kbhit-like function, I call cygwin_select() on file descriptor 0, which in turn calls select_stuff::test_and_set. This function calls cygheap->fdtab.select_read (0, NULL), which calls fhandler_console::select_read (NULL). Since the argument is NULL, this function returns a newly allocated select_record; this pointer is saved in the select_stuff variable sel in cygwin_select (and nowhere else, as far as I can tell). On this variable, we call select_stuff::poll; its result is returned by cygwin_select. So my questions are: 1) Where is the select_record deleted? The select_stuff variable sel will be gone after we exit cygwin_select, and with it the pointer to the select_record we allocated in select_read. 2) To avoid this memory leak, one might consider deleting the select_record just before returning from cygwin_select. However, this does not seem to be the right approach. In select_stuff::poll, we call peek_console, passing a pointer to the select_record. In peek_console, we use the read_ready flag (presumably) to save the state of the console. But, it doesn't seem useful to save the console state if we allocate and delete the select_record each time we call select(). My guess is that cygwin_select should pull in a saved select_record instead of creating a new one. But from where? Thanks in advance, --Michiel. Michiel de Hoon Center for Computational Biology and Bioinformatics Columbia University 1150 St Nicholas Avenue New York, NY 10032 -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/