AutosarC++17_10-A18.5.2

Operators new and delete shall not be called explicitly

Required inputs: IR

Rationale

If a resource returned by operator new is assigned to a raw pointer, then a developer’s mistake, an exception or a return may lead to memory leak.

It is highly recommended to follow RAII design pattern or use manager objects that manage the lifetime of variables with dynamic storage duration, e.g.:

  • std::unique_ptr along with std::make_unique
  • std::shared_ptr along with std::make_shared
  • std::string
  • std::vector

Note: When passing pointers among scopes, use std::unique_ptr to transfer ownershipand std::shared_ptr to share ownership. Use std::weak_ptr to break cycles ofstd::shared_ptr. Functions that do not extend lifetime shall not take parameters assmart pointers.

Exceptions

If the result of explicit resource allocation using new operator is immediately given to a manager object or a RAII class which does not provide a safe alternative for memory allocation, then it is not a violation of the rule.

This rule does not apply to dynamic memory allocation/deallocation performed in user- defined RAII classes and managers.

Example
// $Id: A18-5-2.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <memory>
#include <vector>
std::int32_t Fn1() {
  std::int32_t errorCode{0};

  std::int32_t *ptr =
      new std::int32_t{0}; // Non-compliant - new called explicitly
  // ...
  if (errorCode != 0) {
    throw std::runtime_error{"Error"}; // Memory leak could occur here
  }
  // ...

  if (errorCode != 0) {
    return 1; // Memory leak could occur here
  }
  // ...
  return errorCode; // Memory leak could occur here
}
std::int32_t Fn2() {
  std::int32_t errorCode{0};

  std::unique_ptr<std::int32_t> ptr1 = std::make_unique<std::int32_t>(
      0); // Compliant - alternative for ’new std::int32_t(0)’

  std::unique_ptr<std::int32_t> ptr2(
      new std::int32_t{0}); // Non-compliant - unique_ptr provides make_unique
                            // function which shall be used instead of explicit
                            // new operator

  std::shared_ptr<std::int32_t> ptr3 =
      std::make_shared<std::int32_t>(0); // Compliant

  std::vector<std::int32_t> array; // Compliant
                                   // alternative for dynamic array

  if (errorCode != 0) {
    throw std::runtime_error{"Error"}; // No memory leaks
  }
  // ...
  if (errorCode != 0) {
    return 1; // No memory leaks
  }
  // ...
  return errorCode; // No memory leaks
}
template <typename T> class ObjectManager {
public:
  explicit ObjectManager(T *obj) : object{obj} {}
  ~ObjectManager() { delete object; }
  // Implementation

private:
  T *object;
};
std::int32_t Fn3() {
  std::int32_t errorCode{0};

  ObjectManager<std::int32_t> manager{
      new std::int32_t{0}}; // Compliant by exception
  if (errorCode != 0) {
    throw std::runtime_error{"Error"}; // No memory leak
  }
  // ...
  if (errorCode != 0) {
    return 1; // No memory leak
  }
  // ...
  return errorCode; // No memory leak
}
See Also
  • C++ Core Guidelines [10]: R.11: Avoid calling new and delete explicitly.
  • C++ Core Guidelines [10]: R.12: Immediately give the result of an explicit resource allocation to a manager object.
  • C++ Core Guidelines [10]: ES.60: Avoid new and delete outside resource management functions.
  • C++ Core Guidelines [10]: F.7: For general use, take T* or T& arguments rather than smart pointers.
  • C++ Core Guidelines [10]: F.26: Use a unique_ptr<T> to transfer ownership where a pointer is needed.
  • C++ Core Guidelines [10]: F.27: Use a shared_ptr<T> to share ownership.
  • C++ Core Guidelines [10]: R.20: Use unique_ptr or shared_ptr to represent ownership.
  • C++ Core Guidelines [10]: R.21: Prefer unique_ptr over shared_ptr unless you need to share ownership.
  • C++ Core Guidelines [10]: R.22: Use make_shared() to make shared_ptrs.
  • C++ Core Guidelines [10]: R.23: Use make_unique() to make unique_ptrs.
  • C++ Core Guidelines [10]: R.24: Use std::weak_ptr to break cycles of shared_ptrs.
  • C++ Core Guidelines [10]: R.30: Take smart pointers as parameters only to explicitly express lifetime semantics.
  • C++ Core Guidelines [10]: R.32: Take a unique_ptr<widget> parameter to express that a function assumes ownership of a widget.
  • C++ Core Guidelines [10]: R.33: Take a unique_ptr<widget>& parameter to express that a function reseats the widget.
  • C++ Core Guidelines [10]: R.34: Take a shared_ptr<widget> parameter to express that a function is part owner.
  • C++ Core Guidelines [10]: R.35: Take a shared_ptr<widget>& parameter to express that a function might reseat the shared pointer.
  • C++ Core Guidelines [10]: R.36: Take a const shared_ptr<widget>& parameter to express that it might retain a reference count to the object.
  • C++ Core Guidelines [10]: R.37: Do not pass a pointer or reference obtained from an aliased smart pointer.

Possible Messages

Key

Text

Severity

Disabled

new_delete_call

Operators new and delete shall not be called explicitly.

None

False

Options

allow_for_types_and_subtypes

allow_for_types_and_subtypes : set[bauhaus.analysis.config.QualifiedName] = set()

A set of fully-qualified type names for which the new and delete operators are allowed. This might be necessary if you are working with 3rd party libraries. For Qt, you should instead set the option Frameworks-QtSupport/qt_types.
 

memory_manager_types

memory_manager_types : set[bauhaus.analysis.config.QualifiedName] = set()

Types of objects to be considered memory managers, i.e. calls to new are allowed if they are immediately passed to one of the managers.