[issue8065] Memory leak in readline.get_current_history_length

Alexander Belopolsky report at bugs.python.org
Fri Mar 5 18:06:40 CET 2010


Alexander Belopolsky <alexander.belopolsky at gmail.com> added the comment:

Note the following comment elsewhere in Modules/readline.c:

           /* the history docs don't say so, but the address of state                                                                   
              changes each time history_get_history_state is called                                                                      
              which makes me think it's freshly malloc'd memory...                                                                       
              on the other hand, the address of the last line stays the                                                                  
              same as long as history isn't extended, so it appears to                                                                   
              be malloc'd but managed by the history package... */
           free(state);


It is indeed not documented that state is malloced, but the implementation of history_get_history_state () is as follows:

/* Return the current HISTORY_STATE of the history. */
HISTORY_STATE *
history_get_history_state ()
{
  HISTORY_STATE *state;

  state = (HISTORY_STATE *)xmalloc (sizeof (HISTORY_STATE));
  state->entries = the_history;
  state->offset = history_offset;
  state->length = history_length;
  state->size = history_size;
  state->flags = 0;
  if (history_stifled)
    state->flags |= HS_STIFLED;

  return (state);
}

xmalloc () is an error checking wrapper around malloc, so free () can be used to deallocate the memory.

On the other hand it seems wasteful to request full state in a function that only needs history_length which is directly exported by the readline library.  I am attaching a patch that reads history_length directly.

----------
keywords: +patch
nosy: +Alexander.Belopolsky
Added file: http://bugs.python.org/file16462/issue8065.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8065>
_______________________________________


More information about the Python-bugs-list mailing list