git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 4/4] Make 'unpack_trees()' have a separate source and destination index

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Mar 7, 2008, 21:48 UTC
Message-ID
<alpine.LFD.1.00.0803071339150.3013@woody.linux-foundation.org>
In-Reply-To
<alpine.LNX.1.00.0803071603170.19665@iabervon.org>
On Fri, 7 Mar 2008, Daniel Barkalow wrote:
>
> It looks to me like it's leaking stuff stored in the index it creates if 
> it ends up failing.

Yes, adding a "discard_index(&o->result)" to the failure path sounds like a good idea. Patch appended.

> I'm not entirely sure of the index lifecycle stuff, 
> but it seems like it would be necessary.

Well, "necessary" may not be the right word, because it's a purely internal index thing, so all that happens is a memory leak. But it's certainly easy to do and correct.

That said, the *big* memory leak comes from the fact that we leak the cache_entry things left and right. That's a very traditional leak: because we used to build up the cache entries by just mapping them directly in from the index file (and we emulate that in modern times by allocating them from one big array), we can't actually free them one-by-one.

So doing the "discard_index()" will free the hash tables etc, which is good, and it will free the "istate->alloc" but that is never set on the result because we don't get the result from the index read. So we don't actually free the individual cache entries themselves that got created from the trees.

That's not something new, btw. We never did. But some day we should just add a flag to the cache_entry() that it's a "free one by one" kind, and then we could/should do it. In the meantime, this one-liner will fix *some* of the memory leaks, but not that old traditional one.

		Linus
---
 unpack-trees.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/unpack-trees.c b/unpack-trees.c
index 0cdf198..da68557 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -315,6 +315,7 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str
 
 static int unpack_failed(struct unpack_trees_options *o, const char *message)
 {
+	discard_index(&o->result);
 	if (!o->gently) {
 		if (message)
 			return error(message);
Previous: Daniel BarkalowNext: Junio C Hamano
Message 9 of 11 in “Make unpack_trees() do separate source and destination indexes”
  1. 0/4 Make unpack_trees() do separate source and destination indexesLinus Torvalds, Mar 7, 2008
  2. 3/4 Make 'unpack_trees()' take the index to work on as an argumentLinus Torvalds, Mar 6, 2008
  3. 2/4 Add 'const' where appropriate to index handling functionsLinus Torvalds, Mar 6, 2008
  4. 1/4 Fix tree-walking compare_entry() in the presense of --prefixLinus Torvalds, Mar 6, 2008
  5. 4/4 Make 'unpack_trees()' have a separate source and destination indexLinus Torvalds, Mar 7, 2008
  6. Johannes SchindelinMar 7, 2008
  7. Linus TorvaldsMar 7, 2008
  8. Daniel BarkalowMar 7, 2008
  9. Linus TorvaldsMar 7, 2008
  10. Junio C HamanoMar 7, 2008
  11. Linus TorvaldsMar 7, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.