32 lines
1009 B
Plaintext
32 lines
1009 B
Plaintext
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
|
||
|
||
----
|
||
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
|
||
|
||
----
|
||
void f() {
|
||
scoped_lock lock{myMutex}; // Compliant
|
||
protectedCode();
|
||
// The mutex is correctly released at this point
|
||
}
|
||
----
|
||
|
||
|
||
== See
|
||
|
||
* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#es84-dont-try-to-declare-a-local-variable-with-no-name[{cpp} Core Guidelines ES.84] - Don’t (try to) declare a local variable with no name
|
||
|