--- description: "Learn more about: Warning C26441 NO_UNNAMED_GUARDS" title: Warning C26441 ms.date: 2/7/2023 f1_keywords: ["C26441", "NO_UNNAMED_GUARDS"] helpviewer_keywords: ["C26441"] --- # Warning C26441 > Guard objects must be named (cp.44) ## C++ Core Guidelines [CP.44](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cp44-remember-to-name-your-lock_guards-and-unique_locks): Remember to name your `lock_guard`s and `unique_lock`s ## Remarks The standard library provides locks to help control concurrent access to resources during their lifetime. When you declare a lock object without a name, the compiler creates a temporary object that's immediately destructed rather than one that lives to the end of the enclosing scope. So, failure to assign a lock object to a variable is a mistake that effectively disables the locking mechanism (because temporary variables are transient). This rule catches simple cases of such unintended behavior. This diagnostic only analyzes the standard lock types `std::scoped_lock`, `std::unique_lock`, and `std::lock_guard`. Warning [C26444](c26444.md) covers other unnamed RAII types. The analyzer only analyzes simple calls to constructors. More complex initializer expressions may lead to inaccurate results in the form of missed warnings. Locks passed as arguments to function calls or returned as results of function calls are ignored because the analysis tool is unable to determine if those locks are deliberately trying to protect that function call or if they should be extended. To provide similar protection for types returned by a function call, annotate them with `[[nodiscard]]`. The analyzer ignores locks created as temporaries but assigned to named references to extend their lifetime. Code analysis name: `NO_UNNAMED_GUARDS` ## Example In this example the name of the scoped lock is missing. ```cpp void print_diagnostic(std::string_view text) { auto stream = get_diagnostic_stream(); if (stream) { std::lock_guard{ diagnostic_mutex_ }; // C26441 write_line(stream, text); } } ``` To fix the error, give a name to the lock, which extends its lifetime. ```cpp void print_diagnostic(std::string_view text) { auto stream = get_diagnostic_stream(); if (stream) { std::lock_guard lock{ diagnostic_mutex_ }; write_line(stream, text); } } ``` ## See also [C26444](C26444.md)