IDisposable, guarding against exceptions

Dec 20, 2007 at 10:50 PM

This library doesn't use IDisposable. Instead you have to call objectAddResponse.DataStream.Close() explicitely. This means that, among other things, you'll leak resources when exceptions are thrown.

It's a little tricky as a client to remember to close an object's member field - "objectDeleteResponse.DataStream.Close()". How about implimenting IDisposable so that clients can write "using" clauses? That way I don't need to know about the inner workings of ObjectDeleteResponse. I simply dispose it. (This is Microsoft's recommended practice)

public void DeleteObject(String bucketName, String keyName)
{
ObjectDeleteRequest objectDeleteRequest = new ObjectDeleteRequest(bucketName, keyName);
using (ObjectDeleteResponse objectDeleteResponse = service.ObjectDelete(objectDeleteRequest))
{
}
}

Note also that Response.StreamResponseToFile doesn't have an exception handler around a locally created stream. Here's a cleaner alternative:

using (FileStream fileStream = File.OpenWrite(localfile))
{
this.TransferStream(this.dataStream, fileStream);
}




Coordinator
Apr 11, 2008 at 7:45 PM
The next release is now implementing IDisposable. This should help with the unmanaged resources.

Thanks,
Joel Wetzel
Affirma Consulting