Reputation: 25641
When accessing a resource using Application.GetResourceStream
it returns an instance of the class StreamResourceInfo
, this class does not implement IDisposable.
Should I close\dispose the underlying stream exposed by StreamResourceInfo
when I've finished processing the stream on a WP7 device - I don't want to know about Using
or the Dispose pattern.
An example would be using the method to access a ZIP file which is contained with the ZAP package and once I have unzipped the file I don't need the Stream
instance anymore.
Upvotes: 5
Views: 919
Reputation: 69372
Using
automatically calls the dispose method once the end of the using
scope is reached. So, you could do something like this. Once the end of the scope is reached, your stream will be removed.
using (var stream = Application.GetResourceStream(new Uri("myResource.zip", UriKind.Relative)).Stream)
{
//stream code
}
EDIT - Moving responses from comments to here: The StreamResourceInfo isn't responsible for the closing/disposing of the Stream. Unless you pass that Stream to something else (e.g. a StreamReader), then it's your job to close the stream. Found a link that may be of interest which pretty much goes along with that.
Upvotes: 0
Reputation: 180858
The code example provided here does not make use of using
, Close()
or Dispose()
.
Since the documentation for StreamResourceInfo
doesn't mention Close()
or Dispose()
(and StreamResourceInfo
does not actually inherit from Stream
or anything else that might implement these methods), I think it's safe to just let the object go out of scope and garbage collect naturally.
I suspect that the StreamResourceInfo
class has a finalizer that calls Dispose()
on the underlying Stream
object during garbage collection, but I don't have a copy of Reflector available to me at the moment to verify that. The IDisposable
pattern is quite robust in that regard.
However, if you still feel uncomfortable with that level of uncertainty, you can always call Close()
on the Stream
object.
Upvotes: 3
Reputation: 3638
I don't know about Windows 7 phone specifically, but the standard practice is to dispose the object and it does automatically close the stream if it is opened. Disposing should be the final operation on an object whereas close indicates possibility of reopening. So you should go for dispose in my opinion.
Upvotes: 0