1

I have the following example program

#include <iostream>

class MyClass
{
private:
    int value;
public:
    MyClass(int v) noexcept : value(v) {}
    void displayValue() { std::cout << "The value is " << value; }
};

int main()
{
    auto instance{ std::make_unique<MyClass>(5) };
    instance->displayValue();
}

When I run code analysis i receive the following warning:

main.cpp(15): warning C26486: Don't pass a pointer that may be invalid to a function. Parameter 0 '@instance' in call to 'MyClass::displayValue' may be invalid (lifetime.3).

Can anyone explain to me exactly how I am supposed to be using the std::unique_ptr<MyClass> here to avoid the warning?

Additionally, I receive the following warning in the initialization of the unique_ptr:

main.cpp(14): warning C26414: Move, copy, reassign or reset a local smart pointer 'instance' (r.5).

I can alleviate this issue by wrapping the usage of std::make_unique in std::move but I don't think this should be necessary.

What is the proper way to write this code and avoid the warnings that I'm receiving from the code analyzer?

C. Springer
  • 173
  • 2
  • 12
  • 2
    The code is fine except that `#include` is missing. It should not generate a warning. The `std::move` is redundant and a pessimization. Please show the exact code that generated the warnings and the other warning verbatim as well. – walnut Jan 28 '20 at 19:32
  • 3
    `auto instance{ std::move(std::make_unique(5)) }` should really be `auto instance{std::make_unique(5) }` or `auto instance = std::make_unique(5)` – NathanOliver Jan 28 '20 at 19:34
  • 2
    Sorry, `#include` is also missing. – walnut Jan 28 '20 at 19:37
  • 1
    Sometimes forcing a move is actually detrimental to performance.. – Jesper Juhl Jan 28 '20 at 19:57
  • @walnut I will add the include directives as suggested. I've updated the original question with details regarding the second warning as well. – C. Springer Jan 28 '20 at 20:13
  • @walnut I added the includes as you suggested. For completeness, it did not change the warnings produced by the code analyzer. Even if I can't avoid these warnings, a general understanding of what the analyzer thinks is wrong would be sufficient. – C. Springer Jan 28 '20 at 20:22
  • The warnings are bogus, I would recommend filing a bug report and turning off those warnings for now – M.M Jan 28 '20 at 23:47

1 Answers1

0

In response to warning C26414

I received a reply from Colin Robertson at Microsoft, via GitHub, with the following explanation:

This case comes under the final bullet point in the Remarks section. Unless you're doing something that needs the protection of unique_ptr, declaring it as MyClass instance{5}; avoids some needless overhead. Remember, the warning is just a reminder about a general rule. If you have a good reason for your specific declaration, be confident enough to ignore it.

His reply can also be found here for reference:

https://github.com/MicrosoftDocs/visualstudio-docs/issues/4711

So, basically, we're reminded here to not use heap allocation if it's unnecessary.

In response to warning C26486

Unanswered. I've requested more information via GitHub and am awaiting a reply.

Update 1/29/2020 - Still unanswered. Submitted issue to Microsoft Developer Community

Update 2/3/2020 - Received response from Visual Studio Community (bot, i think?) updating the status of the issue as "Triaged". I guess this means they are prioritizing the issue, maybe? If interested, you can follow the issue here.

C. Springer
  • 173
  • 2
  • 12
  • This is about the second warning, right? What about the first warning? For the first warning, given the code you showed here it is at least worded wrong. – walnut Jan 28 '20 at 21:22
  • @walnut you are correct. I have submitted a question via the same channels on GitHub and am waiting for a response. I will update the answer to try to make it clearer which question is being answered. – C. Springer Jan 28 '20 at 23:01
  • The quoted reply is disappointing, it's basically "to avoid this bug in our product, write different code" . – M.M Jan 28 '20 at 23:48
  • 1
    @M.M The warning makes sense as a code quality warning. It is implementing the enforcement suggestions in [this rule of the C++ core guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-scoped) and given that the heap allocation is indeed unneeded it is a correct warning. – walnut Jan 28 '20 at 23:57
  • @walnut OK, fair enough . The original error text suggests the error is for move/copy/reset the pointer, but the page you link says it is for NOT doing those things – M.M Jan 29 '20 at 00:29
  • 2
    @M.M Yes OP's question wasn't formulated well (it is better now). They added the `std::move` in the previous revision of the question to silence this warning, although the point of the warning is of course not to try to silence it by adding `std::move` all over the place, but to replace the `std::unique_ptr` by an automatic variable. The warning text isn't worded very instructively until one looks up the core guidelines reference. – walnut Jan 29 '20 at 00:33
  • I received a response from another person at Microsoft in regards to the warning C26486. [Here](https://github.com/MicrosoftDocs/visualstudio-docs/issues/4712#issuecomment-579931371) is a link to the reponse. It is uninformative and is pointing me to another possible resource. I'm posting the link here because, quite frankly, I found it entertaining...and then rather disappointing... :/ – C. Springer Jan 29 '20 at 23:53