[Patches] [Patch #102868] fileobject.c:get_line leaks memory when hitting EOF

noreply@sourceforge.net noreply@sourceforge.net
Fri, 15 Dec 2000 16:22:40 -0800


Patch #102868 has been updated. 

Project: python
Category: None
Status: Open
Submitted by: cgw
Assigned to : nobody
Summary: fileobject.c:get_line leaks memory when hitting EOF

Follow-Ups:

Date: 2000-Dec-15 16:22
By: cgw

Comment:
The GNU library documentation says:

     If you set `*LINEPTR' to a null pointer, and `*N' to zero, before
     the call, then `getline' allocates the initial buffer for you by
     calling `malloc'. [...] when `getline' returns,  `*LINEPTR' is a 
    `char *' which points to the text of the line.


It does not explicitly specify what happens if EOF is hit or some other error occurs and -1 is returned.  But one can determine empirically that in this case the memory allocated by "getline" is not free'd.  Thus, every time you call "readline" on a file object and hit EOF, you leak
a little memory, as you will see if you run this little program and watch "top":

while 1:
    file = open('/dev/null', 'r')             
    line = file.readline()
    file.close()
 
(I did this test on a system with GNU glibc version 2.1.3)

The above patch guards against this memory leak.


-------------------------------------------------------

-------------------------------------------------------
For more info, visit:

http://sourceforge.net/patch/?func=detailpatch&patch_id=102868&group_id=5470