Skip to content
  • Jeff King's avatar
    tempfile: remove deactivated list entries · 422a21c6
    Jeff King authored and Junio C Hamano's avatar Junio C Hamano committed
    
    
    Once a "struct tempfile" is added to the global cleanup
    list, it is never removed. This means that its storage must
    remain valid for the lifetime of the program. For single-use
    tempfiles and locks, this isn't a big deal: we just declare
    the struct static. But for library code which may take
    multiple simultaneous locks (like the ref code), they're
    forced to allocate a struct on the heap and leak it.
    
    This is mostly OK in practice. The size of the leak is
    bounded by the number of refs, and most programs exit after
    operating on a fixed number of refs (and allocate
    simultaneous memory proportional to the number of ref
    updates in the first place). But:
    
      1. It isn't hard to imagine a real leak: a program which
         runs for a long time taking a series of ref update
         instructions and fulfilling them one by one. I don't
         think we have such a program now, but it's certainly
         plausible.
    
      2. The leaked entries appear as false positives to
         tools like valgrind.
    
    Let's relax this rule by keeping only "active" tempfiles on
    the list. We can do this easily by moving the list-add
    operation from prepare_tempfile_object to activate_tempfile,
    and adding a deletion in deactivate_tempfile.
    
    Existing callers do not need to be updated immediately.
    They'll continue to leak any tempfile objects they may have
    allocated, but that's no different than the status quo. We
    can clean them up individually.
    
    Signed-off-by: default avatarJeff King <peff@peff.net>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    422a21c6