idisposable

Does the C# compiler automatically dispose of IDisposable objects?

删除回忆录丶 提交于 2019-12-13 17:13:17
问题 Assuming I have a method public static Rectangle DrawRectangle(Vector origin, Vector size) which returns an object of type Rectangle : IDisposable If I call only the method DrawRectangle(origin, size) , but do not assign the return value to a variable myRectangle = DrawRectangle(origin, size) , will the compiler automatically detect this and call DrawRectangle(origin, size).Dispose() , or do I have to do it myself? 回答1: There are only two scenarios I can think of where the compiler

Shared ownership of IDisposable objects in C#

≡放荡痞女 提交于 2019-12-13 12:23:06
问题 Is there any classes in C# which provide shared ownership of IDisposable objects? Something like shared_ptr in c++? And if not, what are best practices here? UPDATE I'm writing a c++/cli wrapper over native lib. And I need release native resources ( MAPI COM interfaces for example, so I need determenistic resource releasing ). Native part: //Message.h class Message { ... }; //MessageSet.h class MessageSet { ... class iterator : public std::iterator<std::forward_iterator_tag, Message*> { ...

Objects implementing IDisposable using another IDisposable

…衆ロ難τιáo~ 提交于 2019-12-13 02:58:46
问题 I try to develop an application in C# and have some concerns with MailMessage object: it implements IDisposable interface, so I use it within using statement. So it implicitly calls Dispose method after. Now, using that object I need to add attachments, which I have converted to byte[] object and I add them as stream. Here's part of code to have better view: using(MailMessage message = new MailMessage("john.smith@gmail.com"){ MemoryStream stream; //here I pass byte array to the stream and

Implementing IDisposable

Deadly 提交于 2019-12-13 02:38:08
问题 VS generates automatically these regionized procedures when I Implement IDisposable: #Region "IDisposable Support" Private disposedValue As Boolean ' To detect redundant calls ' IDisposable Protected Overridable Sub Dispose(disposing As Boolean) If Not Me.disposedValue Then If disposing Then ' TODO: dispose managed state (managed objects). End If ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. ' TODO: set large fields to null. End If Me.disposedValue = True

Thread safe disposable

你离开我真会死。 提交于 2019-12-13 00:53:30
问题 I need to ensure thread safety when disposing an IDisposable resource. The resource is cached multiple times in memory. When entries are evicted from the cache we have a callback that calls Dispose() . Therefore if the resource is cached three times, we'll call Dispose() three times. Is it the responsibility of the IDisposable resource to ensure thread safety or the caller? 回答1: The answer depends on whether the objects you want to dispose adhere to the guidelines or not. If they do some

How pass delegate to a method, where delegates are non static?

放肆的年华 提交于 2019-12-12 18:16:36
问题 I'm just beginning understanding delegates, I have a class that implemens IDisposable: public class MyClass : IDisposable { public delegate int DoSomething(); public int Zero() {return 0;} public int One() {return 1;} public void Dispose() { // Cleanup } } A method (defined in an another class) that is using MyClass: public class AnotherCLass { public static void UseMyClass(MyClass.DoSomething func) { using (var mc = new MyClass()) { // Call the delegate function mc.func(); // <-------- this

Calling dispose() for Font in Windows form

偶尔善良 提交于 2019-12-12 17:03:27
问题 I've been running into the same issue as described in this question. That is, Fortify complained about the creation of font objects, for which the new Font() statements are generated code in the designer.cs files. When looking at profiling results I do get the impreesion I need to do something about this, at least in some cases. This is, of course, an issue only if the develper has assigned a font of his choice to the form in the graphical designer. My plan is to undo that choice and to

Will an IDisposable memory leak if you don't use a using statement?

こ雲淡風輕ζ 提交于 2019-12-12 13:23:40
问题 Will an IDisposable memory leak if you don't use a using statement? And if so, can someone provide a memory leak example if its not much code? 回答1: A correctly-written program which creates an instance of a type that implements IDisposable , and which is not specifically known to be capable of adequately cleaning up after itself when abandoned, must ensure that Dispose is called on that instance before abandoning it. Any program which fails to call Dispose on a type which is not specifically

Why is XmlNodeList disposable?

╄→гoц情女王★ 提交于 2019-12-12 12:00:10
问题 I could not find an answer to this question. Just out of curiosity, why does the XmlNodeList class implement IDisposable in .NET 4.5 when it didn't in the previous versions? 回答1: Most likely for the same reason that IEnumerator<T> implements IDisposable but IEnumerator does not--the earlier version was written before the authors thought of circumstances where one an a implementation might need cleanup, but a factory returning such an implementation might not know about such need. For example,

IDisposable implementation of a non-IDisposable interface

允我心安 提交于 2019-12-12 11:37:25
问题 What is the best way to properly deal with a default implementation of a interface when the interface doesn't inherit from IDisposable ? For example, suppose I want to do public class FooGetter : IDisposable { private IFooProvider fooProvider = MyContainer.GetDefault<IFooProvider>(); ... public void Dispose(){ ... if (fooProvider != null) fooProvider.Dispose(); // obviously has compile error here } } And it just so happens that the default implementation of IFooProvider is IDisposable, but