Always ensure proper memory management when using dynamic allocation. Memory leaks are a critical performance issue that can degrade application performance over time and eventually lead to resource exhaustion.
Always ensure proper memory management when using dynamic allocation. Memory leaks are a critical performance issue that can degrade application performance over time and eventually lead to resource exhaustion.
Instead of using raw pointers with new
without corresponding delete
operations:
Poor example (potential memory leak):
ShapeDescriptor *descriptor = new ShapeDescriptor(dtype, order, shape);
// Missing corresponding delete
Better example:
// Using smart pointer
std::unique_ptr<ShapeDescriptor> descriptor =
std::make_unique<ShapeDescriptor>(dtype, order, shape);
// No explicit delete needed, memory will be freed automatically
// Or if raw pointer is necessary
ShapeDescriptor *descriptor = new ShapeDescriptor(dtype, order, shape);
try {
// use descriptor
// ...
delete descriptor; // Clean up when done
} catch (...) {
delete descriptor; // Clean up on exceptions too
throw;
}
Enter the URL of a public GitHub repository