What is the proper way to free tree elements?

Within the function check_persistent() which is inside of outliner_add_element() there are two operations that seem to need to be undone whenever a TreeElement is allocated. These two operations are:

tselem = BLI_mempool_alloc(soops->treestore);

BKE_outliner_treehash_add_element(soops->treehash, tselem);
.
.

However, I did not see these two operations being undone within outliner_free_tree_element()

I was wondering if the two functions, listed immediately below, should be called before outliner_free_tree_element() is called.

BKE_outliner_treehash_remove_element()
BLI_mempool_free()

If the above two functions are not appropriate then what, if anything, should be done ?

I don’t know this code exactly, but I’d guess that the remove/free functions are not called, because the elements are removed and freed all at once.

Typically, when a mempool is used, you just allocate a bunch of objects in a preallocated memory buffer. Then, when you don’t need these objects anymore, you just free the entire buffer instead of all objects individually.
The same is probably true for the treehash thing.

Hope that helps you figuring out what is really going on.

1 Like

Well, that’s how I assume this issue is being dealt with somewhere in the code, but I’m not actually sure of that yet.

It just, currently, seems like it would be more correct that for every call to MEM_freeN( ""TreeElement"" ) there should immediately precede calls to BKE_outliner_treehash_remove_element() BLI_mempool_free().

This is because tselem = BLI_mempool_alloc(soops->treestore); BKE_outliner_treehash_add_element(soops->treehash, tselem); were both called for every TreeElement allocated.

I, guess, I’ll just take you at your word, and assume nothing further needs to be done when calling MEM_freeN( ""TreeElement"" ) inside outliner_free_tree_element()

Thanks.