VoxelEngine-Cpp icon indicating copy to clipboard operation
VoxelEngine-Cpp copied to clipboard

Upgrade Memory Management to Smart Pointers

Open AlexXZero opened this issue 1 year ago • 3 comments

Objective:

Upgrade the existing memory management in the project by replacing the usage of raw pointers, new, and delete operators with modern C++ smart pointers, specifically std::unique_ptr and std::shared_ptr.

Background:

The current codebase utilizes manual memory management through the use of new and delete operators, which can lead to potential memory leaks, dangling pointers, and increased code complexity. By transitioning to smart pointers, we aim to enhance code safety, readability, and maintainability while reducing the risk of memory-related issues.

Benefits:

  1. Automatic Resource Management: Smart pointers automatically handle memory deallocation when the object is no longer in use, minimizing the risk of memory leaks.
  2. Improved Code Readability: The use of smart pointers makes code more expressive and self-documenting, reducing the likelihood of manual memory management errors.
  3. Easier Maintenance: Smart pointers simplify resource management, making it easier to maintain and update the codebase over time.

Tasks:

  1. Identify areas in the codebase where manual memory management (e.g., new and delete operators) is prevalent.
  2. Replace raw pointers with std::unique_ptr where exclusive ownership is appropriate.
  3. Replace raw pointers with std::shared_ptr where shared ownership is required.
  4. Update existing code to accommodate the changes.
  5. Perform thorough testing to ensure the new smart pointer-based memory management does not introduce any regressions.

Considerations:

  1. Review and handle cases where custom deleters are used with new and delete.
  2. Assess potential performance implications, although modern C++ compilers are optimized for efficient smart pointer usage.
  3. Collaborate with the team to address any concerns or challenges encountered during the transition.

Completion Criteria:

  1. All instances of new and delete are replaced with appropriate smart pointers.
  2. Code compiles successfully after the changes.
  3. No memory leaks or related issues are detected during thorough testing.

AlexXZero avatar Jan 14 '24 21:01 AlexXZero

Extra task: replace return nullptr with return std::nullopt when method returns non-value.

AlexXZero avatar Jan 14 '24 22:01 AlexXZero

@MihailRis Just want to let you know that during refactoring raw pointers I found at least one memory leak: World class is only allocated, but never free the memory. I'm going to fix it with saving World in Level class (as it was done before) using std::unique_ptr, which will be moved (using std::move) from World::create and World::load methods.

AlexXZero avatar Jan 16 '24 21:01 AlexXZero

I found that it free memory at LevelScreen destructor, which is super not obvious, so I will update it any way.

AlexXZero avatar Jan 16 '24 22:01 AlexXZero