[cmake-developers] Proposal: Using smart pointers to own dynamically allocated memory
Tushar Maheshwari
tushar27192 at gmail.com
Fri Sep 13 12:38:21 EDT 2019
Hi,
I am a C++ developer and a modern-C++ enthusiast. In the interest of
modernizing the codebase, I propose using smart pointers to handle
dynamically allocated memory. I would like to get the feedback from
the developers if this change is planned/attempted/desirable or
inapplicable for CMake.
Currently, in the master branch (limiting to the Source directory):
- delete expression appears 127 times (2 of those are false positives).
$ grep -Er 'delete(\[\])? \S+;' Source | wc -l
127
- cmDeleteAll function (a helper to delete a Range of objects) is used 40 times.
$ grep -Er 'cmDeleteAll\(.+\);' Source | wc -l
40
- [Skipping the `free` stats. I can investigate that if required.]
Many of these are great candidates for `std::unique_ptr`. This can
reduce the destructors of many classes to default, and in some cases
achieve the rule of zero. This aligns well with the "Resource
management" section of the CppCoreGuidelines.
However, it might be impractical to replace some occurrences, like the
interfaces with C libraries.
I have pushed some sample commits to
https://gitlab.kitware.com/tusharpm/cmake/commits/smart_mem.
One drawback I noticed while changing some members to use smart
pointers is the boilerplate required to expose them to the callers.
There might be a cleaner way than passing references to smart
pointers. I would like to discuss those options also, if possible.
If this is something I can pursue, I would appreciate a review of my
changes to better suit the project.
Thanks,
Tushar
More information about the cmake-developers
mailing list