idisposable

Explicit implementation of IDisposable

我是研究僧i 提交于 2019-12-03 17:13:18
问题 Although there are quite a lot of Q&As regarding IDisposable to be found on SO, I haven't found an answer to this yet: I usually follow the practice that when one of my classes owns an IDisposable object then it also implements IDisposable and calls Dispose on the owned object. However recently I came across a class which implemented IDisposable explicitly thus preventing me from directly calling Dispose forcing me to cast it which I found annoying and unnecessary. So the question: Why and

How do I force release memory occupied by MemoryStream?

元气小坏坏 提交于 2019-12-03 17:11:34
问题 I have the following code: const int bufferSize = 1024 * 1024; var buffer = new byte[bufferSize]; for (int i = 0; i < 10; i++) { const int writesCount = 400; using (var stream = new MemoryStream(writesCount * bufferSize)) { for (int j = 0; j < writesCount; j++) { stream.Write(buffer, 0, buffer.Length); } stream.Close(); } } which I run on a 32-bit machine. The first iteration finishes just fine and then on the next iteration I get a System.OutOfMemoryException exception on the line that new s

Best practices for handling IDisposable

假如想象 提交于 2019-12-03 16:39:01
问题 I have a class hierarchy, each member of which may create IDisposable objects. I added a List<IDisposable> property to the base class in this hierarchy, to which I add any disposable objects on creation. The root Dispose method iterates through this list and calls Dispose for each item in its list and clears the list. In the application, I explicitly call the top object's Dispose method, causing disposal to cascade through the hierarchy. This works, but is there a better way? Am I unwittingly

How to find all Classes implementing IDisposable?

a 夏天 提交于 2019-12-03 15:33:47
问题 I am working on a large project, and one of my tasks is to remove possible memory leaks. In my code, I have noticed several IDisposable items not being disposed of, and have fixed that. However, that leads me to a more basic question, how do I find all classes used in my project that implement IDisposable? (Not custom-created classes but standard Library classes that have been used). I have already found one less-than-obvious class that implements IDisposable ( DataTable implements

What to do with IObservers being disposed?

穿精又带淫゛_ 提交于 2019-12-03 14:33:44
I'm using Reactive Extensions for easy event handling in my ViewModels (Silverlight and/or Wp7 apps). For sake of simplicity let's say I have a line like this in the ctor of my VM: Observable.FromEvent<PropertyChangedEventArgs>( h => MyObject.PropertyChanged += h, h => MyObject.PropertyChanged -= h) .Where(e=>e.PropertyName == "Title") .Throttle(TimeSpan.FromSeconds(0.5)) .Subscribe(e=>{/*do something*/}); this returns an IDisposable object, which if disposed will unsubscribe. (Am I right in this assumption?) If I hold no reference to it, sooner or later it will be collected, and my handler

How to Dispose DbContext(or object) in asp.net mvc3 App when Ninject is used as dependency resolver

倖福魔咒の 提交于 2019-12-03 13:56:30
问题 For this Demo I have created a fake Database+repository as below Fake Db + Repository public interface IDemoRepository { string[] GetUsers(); } public class DemoRepository : IDemoRepository, IDisposable { public string[] GetUsers() { string[] Users = { "Robert","Linda","Jack"}; return Users; } public void Dispose() { //do nothing throw new Exception("Disposed is called"); } } My Controller looks this public class TestController:Controller { protected IDemoRepository _repository; public

How to manage IDisposable Objects that are cached?

依然范特西╮ 提交于 2019-12-03 11:59:05
I have an object that is expensive to create, which uses some unmanaged resources that must be explicitly freed when done with and so implement IDisposable(). I would like a cache for instance of these expensive resources so that the creation cost is minimized, but I am having trouble knowing how to deal with the disposal. If the methods that use the objects are responsible for the disposal then I end up with disposed instances in the cache, which then have to be recreated, defeating the point of the cache. If I don't dispose the objects in the methods that use them then they never get

Avoiding disposing system-defined Pen and Brush instances

大憨熊 提交于 2019-12-03 11:48:10
I understand it is best practise to call Dispose() on instances of Pen and Brush , except if they've been set to the system-predefined values (eg. System.Drawing.Brushes , System.Drawing.Pens or System.Drawing.SystemBrushes ) Trying to dispose a system-defined resource results in an exception being thrown. It doesn't appear to be obvious how you can detect (apart from wrapping the Dispose() call in a try/catch) whether one of these resources is referencing a system-defined or user-defined value. There is no requirement to call Dispose . The purpose of garbage collection is to eliminate these

Struct and IDisposable

左心房为你撑大大i 提交于 2019-12-03 11:40:21
问题 I wonder why does it not compile? public static void Main(string[] args) { using (MyStruct sss = new MyStruct()) { sss.s = "fsdfd";// Cannot modify members of 'sss' because it is a 'using variable' //sss.Set(12); //but it's ok } } public struct MyStruct : IDisposable { public int n; public string s; public void Set(int n) { this.n = n; } public void Dispose() { Console.WriteLine("dispose"); } } UPDATE : But it works perfect. Why? public static void Main(string[] args) { using (MyClass sss =

Correct way to close WCF 4 channels effectively

青春壹個敷衍的年華 提交于 2019-12-03 11:28:11
I am using the following ways to close the WCF 4 channels. Is this right way to do it? using (IService channel = CustomChannelFactory<IService>.CreateConfigurationChannel()) { channel.Open(); //do stuff }// channels disposes off?? Although not strictly directed at the channel, you can do: ChannelFactory<IMyService> channelFactory = null; try { channelFactory = new ChannelFactory<IMyService>(); channelFactory.Open(); // Do work... channelFactory.Close(); } catch (CommunicationException) { if (channelFactory != null) { channelFactory.Abort(); } } catch (TimeoutException) { if (channelFactory !=