Closed julianhartmer closed 2 years ago
Interesting. This could also effectively deprecate the cvector_free_each_and_free
function. I'll take a look at it :-)
Thanks for your feedback, I updated the commit.
Now, the formatting is correct again
I added one more comment but otherwise is looking good!
I accidentally pushed my changes to fix alignment issues on this branch. Reverted the commit immediately. I think it's best to keep this two issues seperate to prevent this PR from becoming a monster.
OK, I think this is looking pretty good!
I may decide to make a follow up PR myself to make the destructor feature opt in/out, but for now this is looking like it "just works" 👍🏻
Add function pointer that mimcs the element's destructor. The vector may be used with complex data structures that hold dynamically allocated memory. To prevent leaking this memory when elements are removed, I introduce a new field at the start of the cvector meta-data called
elem_destructor()
. Each time a element is removed, the function is called to prevent memory leaks. Initially, the function pointer is set to NULL. If that's the case, then no destructor is called, ensuring backwards compatibility.This PR also already contains the commits from PR 36, so please land this one first.
Here is the test I wrote to check the functionality: