简体   繁体   English

解决此方案中的代码分析“CA2000”规则?

[英]Resolving code analysis “CA2000” rule in this scenario?

I'm running a full code analysis on my project, and it says it has 500 issues. 我正在对我的项目运行完整的代码分析,它说它有500个问题。 I've boiled it down to 300 now, but I'm struggling with an issue I can't seem to find a solution for. 我现在把它煮到了300,但我正在努力解决一个我似乎无法找到解决方案的问题。

The rule CA2000 states: CA2000规则规定:

If a disposable object is not explicitly disposed before all references to it are out of scope, the object will be disposed at some indeterminate time when the garbage collector runs the finalizer of the object. 如果在对所有对象的所有引用都超出范围之前没有明确地处置一次性对象,则当垃圾收集器运行对象的终结器时,该对象将在某个不确定的时间处置。 Because an exceptional event might occur that will prevent the finalizer of the object from running, the object should be explicitly disposed instead. 由于可能会发生异常事件以阻止对象的终结器运行,因此应该明确地处理该对象。

More information on the rule can be found on the page linked to above. 有关规则的更多信息可以在链接到上面的页面上找到。

The code that the rule is failing on is this: 规则失败的代码是:

internal Window(Game game, Control parent, string title, bool visible)
    : base(game, parent, visible, new ScreenspaceRectangle(game, Color.Black, Vector.Zero, Vector.Zero))
{
}

And the description is: 描述是:

CA2000 : Microsoft.Reliability : In method 'Window.Window(Game, Control, string, bool)', call System.IDisposable.Dispose on object 'new ScreenspaceRectangle(game, Color.Black, Vector.Zero, Vector.Zero)' before all references to it are out of scope. CA2000:Microsoft.Reliability:在方法'Window.Window(Game,Control,string,bool)'中,在对象'new ScreenspaceRectangle(game,Color.Black,Vector.Zero,Vector.Zero)上调用System.IDisposable.Dispose'在所有引用都超出范围之前。

I understand that this issue can be resolved normally by using a "using" statement around the object being created, to make sure that it is always properly disposed. 我知道通过在正在创建的对象周围使用“using”语句来正常解决此问题,以确保始终正确处理它。 But how do I solve it in this case? 但是在这种情况下如何解决呢?

Assuming that the Window class is your custom class, you should ensure that the base class constructor is storing the reference of ScreenspaceRectangle if it requires it outside the constructor and it implements IDisposable and disposes the instance of ScreenspaceRectangle in the Dispose method. 假设Window类是您的自定义类,您应该确保基类构造函数存储ScreenspaceRectangle的引用(如果它在构造函数外部需要它)并且它实现了IDisposable并在Dispose方法中Dispose ScreenspaceRectangle的实例。

Otherwise ensure that the object is being disposed in the base class constructor. 否则,请确保将对象放置在基类构造函数中。

One annoying limitation of c#/vb.net is that they don't allow chained constructors or field initializers to be wrapped in a try-catch or try-finally block. c#/ vb.net的一个令人讨厌的限制是它们不允许将链式构造函数或字段初始化程序包装在try-catch或try-finally块中。 If the creation of object Foo requires the creation of some other IDisposable objects which Foo will be responsible for disposing, it can be difficult to ensure that those objects will get disposed if an exception is thrown from the constructor of Foo, its subtype, or its supertype. 如果对象Foo的创建需要创建一些Foo将负责处理的其他IDisposable对象,那么如果从Foo的构造函数,其子类型或其中抛出异常,则很难确保这些对象将被处置掉。超类型。 The cleanest way I know of to deal with this is to use a protected constructor, wrapped in factory methods that will create a "disposables manager" instance and pass it through the constructor chain. 我知道处理这个问题最干净的方法是使用受保护的构造函数,它包含在工厂方法中,这些方法将创建一个“一次性管理器”实例并将其传递给构造函数链。 Anything that would create an IDisposable should add it to the disposables manager; 任何会产生IDisposable的东西都应该把它添加到一次性用品经理那里; if the constructor throws an exception, the disposables manager will delete all registered disposables. 如果构造函数抛出异常,则一次性使用者管理器将删除所有已注册的一次性用品。

One advantage of this approach is that it allows the cleanup of nested IDisposables to be handled by the code that creates them, thus minimizing the danger of object-creation and cleanup code getting out of sync. 这种方法的一个优点是它允许嵌套IDisposable的清理由创建它们的代码处理,从而最大限度地降低了对象创建和清理代码不同步的危险。 One caveat is that one must either use a thread-static field to keep track of the disposables manager, or else pass it through every step of the constructor chain. 需要注意的是,必须使用线程静态字段来跟踪一次性管理器,或者将其传递给构造函数链的每一步。 The former approach feels icky, but has the advantage of allowing field initializers to safely create IDisposable objects. 前一种方法感觉很蹩脚,但具有允许字段初始化器安全地创建IDisposable对象的优点。

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

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