AutosarC++18_03-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: Functions that do not extend lifetime shall not take parameters as smart pointers, see A8-4-11.

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.

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.