简体   繁体   English

Visual Studio Community 2019 中的 C++ 代码分析产生警告 C26486 和 C26414

[英]C++ Code Analysis in Visual Studio Community 2019 produces warnings C26486 and C26414

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. main.cpp(15):警告 C26486:不要将可能无效的指针传递给函数。 Parameter 0 '@instance' in call to 'MyClass::displayValue' may be invalid (lifetime.3).调用 'MyClass::displayValue' 的参数 0 '@instance' 可能无效 (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?任何人都可以向我解释我应该如何在这里使用std::unique_ptr<MyClass>来避免警告?

Additionally, I receive the following warning in the initialization of the unique_ptr:此外,我在 unique_ptr 的初始化中收到以下警告:

main.cpp(14): warning C26414: Move, copy, reassign or reset a local smart pointer 'instance' (r.5). main.cpp(14):警告 C26414:移动、复制、重新分配或重置本地智能指针“实例”(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.我可以通过在std::move包装std::make_unique的使用来缓解这个问题,但我认为这不是必要的。

What is the proper way to write this code and avoid the warnings that I'm receiving from the code analyzer?编写此代码并避免我从代码分析器收到的警告的正确方法是什么?

In response to warning C26414响应警告 C26414

I received a reply from Colin Robertson at Microsoft, via GitHub, with the following explanation:我通过 GitHub 收到了来自 Microsoft 的 Colin Robertson 的回复,其中有以下解释:

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};除非你正在做一些需要 unique_ptr 保护的事情,否则将它声明为 MyClass 实例{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 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响应警告 C26486

Unanswered.未答复。 I've requested more information via GitHub and am awaiting a reply.我已经通过 GitHub 请求了更多信息,正在等待回复。

Update 1/29/2020 - Still unanswered. 2020 年 1 月 29 日更新 - 仍未得到答复。 Submitted issue to Microsoft Developer Community向 Microsoft 开发人员社区提交问题

Update 2/3/2020 - Received response from Visual Studio Community (bot, i think?) updating the status of the issue as "Triaged". 2020 年 2 月 3 日更新 - 收到 Visual Studio 社区(我认为是机器人?)的回复,将问题状态更新为“已分类”。 I guess this means they are prioritizing the issue, maybe?我想这意味着他们正在优先考虑这个问题,也许吧? If interested, you can follow the issue here .如果有兴趣,您可以在此处关注问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM