kvrocks
kvrocks copied to clipboard
Use `unique_ptr` to eliminate some trivial manual deallocation
Search before asking
- [X] I had searched in the issues and found no similar issues.
Motivation
There are many manual memory (or other system resources like file) allocation and deallocation such as new/delete in kvrocks.
Memory management is a complex topic (after RC/tracing GC is involved quickly) and there are some smart pointers like unique_ptr, shared_ptr in C++ to abstract a specific way to manage resources.
For some trivial scenario which do not require to share objects, we can do it via unique_ptr, which is a zero-overhead abstraction.
We can switch some manual deallocations to unique_ptr to make the code more maintainable and less prone to memory leaks.
NOTE: We try to use unique_ptr instead of creating our own RAII class types because unique_ptr has good semantics: it prohibits copy construction and only allows move construction, which keeps us from accidentally sharing objects and causing Use After Free or Double Free. (And we should carefully to retrieve raw pointers via get() by checking that the callee do not require ownership)
Solution
No response
Are you willing to submit a PR?
- [ ] I'm willing to submit a PR!
I'd like to do it.
I'd like to do it.
Thanks, feel free to submit a PR.
Since this issue may be divided into many PRs, everyone can directly submit a PR for a specific part (no need to reply this issue), so I think it would be better not to assign this issue to a specific person.
@PragmaTwice is there any remaining works and do you want to continue driving this effort?
I think most of trivial manual deallocation is removed, but there still remains some raw memory allocation and fd allocation.
@PragmaTwice is there a way we can find and list them out here? Otherwise, it seems this proposal stalls.
Closed. Will create new issue if more manual deallocation is found.