rspec/rules/S5025/cfamily/rule.adoc

74 lines
2.4 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
If you manage memory manually, it's your responsibility to ``++delete++`` all memory created with ``++new++``, and to make sure it's ``++delete++``d once and only once. Ensuring this is done is error-prone, especially when your function can have early exit points.
Fortunately, the {cpp} language provides tools that automatically manage memory for you. Using them systematically makes the code simpler and more robust without sacrificing performance.
This rule raises an issue when you use:
* ``++new++`` - you should prefer a factory function that returns a smart pointer, such as ``++std::make_unique++`` or, if shared ownership is required, ``++std::make_shared++``,
* ``++new[]++`` - you should prefer a container class, such as ``++std::vector++``,
* ``++delete++`` or ``++delete[]++`` - if you followed the previous advice, there is no need to manually release memory.
If your compiler does not support ``++make_unique++``, it's easy to write your own:
----
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
----
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
void f() {
auto c = new Circle(0, 0, 5);
c->draw();
delete c;
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
void f() {
auto c = make_unique<Circle>(0, 0, 5);
c->draw();
unique_ptr<Circle> c2{new Circle(0, 0, 5)}; // Clumsy, but still compliant by exception
}
----
=== Exceptions
2021-04-28 16:49:39 +02:00
If the result of a new is immediately passed as an argument to a function, we assume that the function takes ownership of the newly created object, and won't raise an issue.
== Resources
2021-04-28 16:49:39 +02:00
* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#r11-avoid-calling-new-and-delete-explicitly[{cpp} Core Guidelines R.11] - Avoid calling new and delete explicitly
* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#c149-use-unique_ptr-or-shared_ptr-to-avoid-forgetting-to-delete-objects-created-using-new[{cpp} Core Guidelines C.149] - Use unique_ptr or shared_ptr to avoid forgetting to delete objects created using new
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]