kvrocks icon indicating copy to clipboard operation
kvrocks copied to clipboard

Use `unique_ptr` to eliminate some trivial manual deallocation

Open PragmaTwice opened this issue 3 years ago • 4 comments

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!

PragmaTwice avatar Jun 24 '22 15:06 PragmaTwice

I'd like to do it.

jackwener avatar Jun 24 '22 15:06 jackwener

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 avatar Jun 24 '22 15:06 PragmaTwice

@PragmaTwice is there any remaining works and do you want to continue driving this effort?

tisonkun avatar Sep 11 '22 03:09 tisonkun

I think most of trivial manual deallocation is removed, but there still remains some raw memory allocation and fd allocation.

PragmaTwice avatar Sep 11 '22 10:09 PragmaTwice

@PragmaTwice is there a way we can find and list them out here? Otherwise, it seems this proposal stalls.

tisonkun avatar Oct 23 '22 03:10 tisonkun

Closed. Will create new issue if more manual deallocation is found.

PragmaTwice avatar Oct 23 '22 07:10 PragmaTwice