55 lines
1.3 KiB
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
The RAII idiom associates the lifetime of a resource with the lifetime of an object: The resource is acquired when the object is created, and released when it is destroyed.
If the object that controls the resource lifetime is a temporary, chances are it will get destroyed while the resource should still be in use, leading to resource corruption. This rule detects temporaries that look like RAII objects.
=== 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() {
scoped_lock{myMutex}; // Noncompliant. The mutex will be locked then immediately unlocked
protectedCode(); // This code is not protected by the mutex
}
----
=== 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() {
scoped_lock lock{myMutex}; // Compliant
protectedCode();
// The mutex is correctly released at this point
}
----
== Resources
2021-04-28 16:49:39 +02:00
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#es84-dont-try-to-declare-a-local-variable-with-no-name[ES.84: Don't try to declare a local variable with no name]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Name this temporary XXX object if you want to use it in for RAII.
'''
== Comments And Links
(visible only on this page)
=== is related to: S5506
endif::env-github,rspecator-view[]