The effect of implementing IDisposable on classes that it doesn't need?

I check my static analysis again and I get 100 seconds nags on

Label foo = new Label(); //Where this is an ASP.NET web forms label

Do I need to call these web form tags? Also, how late in the page life cycle can you safely call Dispose on them? If a class implements IDisposable but doesn't actually do anything in it, is there any harm in not calling dispose?

(Yes, I already read that closing connections is good, and using blocks is good too.)

A DataSet is another class that seems to implement IDisposable for unexplained reasons.

+3
source share
4 answers

Dispose() -, . , , , using, , , .

TestDispose.ascx.cs:

public partial class TestDispose : System.Web.UI.UserControl {
  public void override Dispose() {
    // Set breakpoint here
    base.Dispose();
  }
}

TestPage.aspx.cs:

public partial class TestPage : System.Web.UI.Page {
  public void override OnInit() {
    // test will be disposed of when the page is destroyed
    TestDispose test = new TestDispose();
    test.ID = "TestDispose";
    this.Controls.Add(test);

    // test1 will not be disposed of because it was not added
    // to the page control tree.
    TestDispose test1 = new TestDispose();
    test1.ID = "TestDispose1";
  }
}

TestDispose.Dispose(), , Dispose() . , "Dispose() ".

Update

, , , - . ASP.Net - , Dispose() on .

Reflector, , , Dispose() :

System.Web.UI.Page.AspCompatBeginProcessRequest
-> System.Web.UI.Page.ProcessRequest
   -> System.Web.UI.Page.ProcessRequestCleanup
      -> System.Web.UI.Control.UnloadRecursive

System.Web.UI.Control.UnloadRecursive Controls Dispose() Control. , System.Web.UI.UserControl, IDisposable.

, , , . / - , .

+6

IDisposable, , , dispose?

, dispose, . , , ) b)

DataSet - , , , IDisposable .

DataSet , , - , MarshalByValueComponent.

, : , , , (, Page.Unload ). , , , , (ala SharePoint).

+3

. Dispose(), . - .

: . , Label.

, ( ) , (Finalizer). Dispose() , . .

+1
source

Yes, if the Dispose () method does nothing, then the Dispose call will not be safe. If your static analysis tool actually types them, you should probably turn it off if you plan on working with Winforms. IMO, this is not a very good analysis, since there are many scenarios in which it is known that Dispose () is a chain and only a root call is needed (i.e. Streams / Readers).

0
source

Source: https://habr.com/ru/post/1757718/


All Articles