idisposable

Does my code properly clean up its List<MemoryStream>?

I've got a third-party component that does PDF file manipulation. Whenever I need to perform operations I retrieve the PDF documents from a document store (database, SharePoint, filesystem, etc.). To make things a little consistent I pass the PDF documents around as a byte[]. This 3rd party component expects a MemoryStream[] (MemorySt...

How do you prevent IDisposable from spreading to all your classes?

Start with these simple classes... Let's say I have a simple set of classes like this: class Bus { Driver busDriver = new Driver(); } class Driver { Shoe[] shoes = { new Shoe(), new Shoe() }; } class Shoe { Shoelace lace = new Shoelace(); } class Shoelace { bool tied = false; } A Bus has a Driver, the Driver has t...

returning in the middle of a using block.

Something like: using (IDisposable disposable = GetSomeDisposable()) { //..... //...... return Stg(); } I believe it is not a proper place for a return statement, is it? ...

Handling ObjectDisposedException correctly in an IDisposable class hierarchy

When implementing IDisposable correctly, most implementations, including the framework guidelines, suggest including a private bool disposed; member in order to safely allow multiple calls to Dispose(), Dispose(bool) as well as to throw ObjectDisposedException when appropriate. This works fine for a single class. However, when you subc...

Dispose on user controls, really meant to edit the .designer.cs file?

For a user control with internal data structures that must be disposed, is the correct place to add that code to the Dispose method in the .designer.cs file, or is there an event or something we're meant to use instead? Edit: This is a winforms user control. ...

Who Disposes of an IDisposable public property?

If I have a SomeDisposableObject class which implements IDisposable: class SomeDisposableObject : IDisposable { public void Dispose() { // Do some important disposal work. } } And I have another class called AContainer, which has an instance of SomeDisposableObject as a public property: class AContainer { Som...

Is an object still disposed if I return within a using statement?

Dupe of http://stackoverflow.com/questions/662773/returning-in-the-middle-of-a-using-block Question title is fairly obvious I think, so given the following code is the SecurityDisabler disposed if true is returned? public bool CreateProxyItem(string name, Sitecore.Data.ID sourceID, Sitecore.Data.ID targetID) { // create an instance...

What do you think of my IDisposable pattern implementation?

What do you think of the following IDisposable pattern implementation? public class Connection : IDisposable { private Socket _socket; public bool IsConnected() { if (_socket.Poll(1, SelectMode.SelectRead) && _socket.Available == 0) return false; return true; } public void Disconnect() ...

Will using work on null?

Will the following code work if resource doesn't implement IDisposable? T resource = new T(); using (resource as IDisposable) { ... } ...

How can I dispose System.Xml.XmlWriter in PowerShell

I am trying to dispose XmlWriter object: try { [System.Xml.XmlWriter] $writer = [System.Xml.XmlWriter]::Create('c:\some.xml') } finally { $writer.Dispose() } Error: Method invocation failed because [System.Xml.XmlWellFormedWriter] doesn't contain a method named 'Dispose'. On the other side: $writer -is [IDisposab...

Why does VS2005/VB.NET implement the IDisposable interface with a Dispose(disposing as boolean) overload?

Recently I needed to compare a suggested pattern for IDisposable and object finalization with the auto-generated one we which VS2005/VB.NET provide. We have used the auto-generated one a fair bit, but after looking it the two side by side I had a number of questions about the VB.NET implementation... For reference, here is the IDE's im...

How does GC and IDispose work in C#?

I remember i was loading in images by streaming it from the net straight into a bitmap. close the stream, return the bitmap and held it in an image control. I excepted when i did = loadPicture() the first bitmap would be freed like a smart pointer would do in C++. But it didnt and i consumed a lot of ram until i called dispose. So my qu...

how to lump up a IEnumerable<IDisposable>

I have an IEnumerable<IDisposable> collection that I need to dispose of at a given point. The issue is that for some cases all the object will be of one type and disposing of the objects needs to be done by collecting some data from them and making a single RPC call. In other cases, the objects will be of another type and I just need to ...

Passing an IDisposable object by reference causes an error?

I am trying to create a general method for disposing an object that implements IDisposable, called DisposeObject() To make sure I am disposing an object pointed by original reference, I am trying to pass an object by reference. But I am getting a compilation error that says The 'ref' argument type doesn't match parameter type In ...

Will dispose be called for anonymous variables?

For example, int myResult= (new UnmanagedResourceUsingMemorySuckingPig()).GetThingsDone(id); There is no using block, no obvious way to use a using block, no obvious way to call Dispose(). And of course UnmanagedResourceUsingMemorySuckingPig does implement IDisposable. ...

Is there a Using pattern that does not rely on IDisposable ?

I am wanting to create an internal messaging system that can tell me the duration of some code being called. I was thinking for ease of use, to make the SystemMessage class implement IDisposable. I would set a time stamp during the SystemMessage's constructor and if the Dispose was called, I could figure out the duration. The prob...

C# Linq-to-Sql - Should DataContext be disposed using IDisposable

I have several methods that deal with DB and all of them start by calling FaierDbDataContext db = new FaierDbDataContext(); Since the Linq2Sql DataContext object implements IDisposable, should this be used with "using"? using (FaierDbDataContext db = new FaierDbDataContext()) { // use db here } What are the implications of usin...

Is this IDisposable implementation correct?

I can never remember all the rules for implementing the IDisposable interface, so I tried to come up with a base class that takes care of all of this and makes IDisposable easy to implement. I just wanted to hear your opinion if this implementation is ok as it is or whether you see something I could improve. The user of this base class i...

Xml Serialization without Disposing

using (var file_stream = File.Create("users.xml")) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close(); } Using the above code works perfectly. However, when I shorten it to: var serializer = new...

Is this correct for disposing an object using IDisposable

I have a class that implements the IDisposable interface. I am using a webclient to download some data using the AsyncDownloadString. I am wondering have I correctly declared my event handlers in the constructor and within the using statement of the web client? And is this correct way to remove the event handlers in the Dispose method?...