c# - Why does visual studio code analysis warn me that this object can be disposed twice in this code (when it can't) -


code is:

using (memorystream memorystream = new memorystream(bytes)) {     using (binarywriter writer = new binarywriter(memorystream))     {         writer.write((double)100.0);     }     return memorystream.toarray(); } 

isn't above code appropriate dispose of both object?

is code analysis @ useful? other garbage information variables names , namespaces seems complain lot of things not reality. thinking maybe useful , missing point.

okay address concerns whether memorystream disposed of or not (its not) here example vs code analysis gives me exact same warning. nothing getting disposed of here

public class myclass : idisposable {    public void dosomethingelse()     {      }     #region idisposable members    public void dispose()    {        throw new notimplementedexception();    }    #endregion }  public class myotherclass : idisposable {     public myotherclass(myclass mc)     {      }     public void dosomething() { } }  public void foo() {     using (myclass mc = new myclass())     {         using (myotherclass otherclass = new myotherclass(mc))         {             otherclass.dosomething();         }         mc.dosomethingelse();     } } 

on "if stream disposed how can call memorystream.toarray()" part of question:

memeorystream.dispose not release internal buffer. thing blocking stream methods (like read) throwing "object disposed" exception. i.e. following code valid (usually similar code accessing lower level storage can written other other stream objects manage sort of storage):

memorystream memorystream = new memorystream(); using (memorystream) {  // write stream } // note memorystream.write fail here exception, // calls storage (toarray , getbuffer) make sense @ point. var data = memorystream.toarray(); 

Comments

Post a Comment

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

html - Building a fluid horizontal navigation -