5

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.

AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152

3 Answers3

3

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
0

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.

Varun K
  • 3,593
  • 2
  • 25
  • 26
  • thanks I understand the dispose pattern, the problem is the StreamResourceInfo does not have a dispose method it's self, but the contained Stream does. – AwkwardCoder Jun 01 '11 at 12:16
  • Then you have no option but to call close. The stream should ideally be implemented in such a way that it should call dispose of underlying stream once it is closed in this case. are you able to access underlying stream anyhow? – Varun K Jun 01 '11 at 12:28
0

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.

Community
  • 1
  • 1
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • @AwkwardCoder - I saw it, but I don't see what the problem is. `StreamResourceInfo` is not reponsible for the stream - you are. Therefore, the onus is on you to close/dispose the stream. `Using`, or manually calling `Dispose` on the stream is the way to do that. What are you trying to achieve? – keyboardP Jun 01 '11 at 15:59
  • "Using automatically calls the dispose method once the end of the using scope is reached. " - ever thought i might know this... I am asking about a specific class in the framework return by a call to Application.GetResourceStream. – AwkwardCoder Jun 01 '11 at 16:10
  • @AwkwardCoder: Downvoted for adding context? Lets not forget, other people read this post as well who may not know this. I'd like this to think this is a professional forum, so it's a shame you have to resort to petty actions and sarcasm. I simply explained that 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. – keyboardP Jun 01 '11 at 16:13
  • Have you read the guidelines for implementing a class which internally creates and exposes a property which needs disposing? – AwkwardCoder Jun 01 '11 at 16:18
  • I suggest you read the law of demeter and the principles of object orientation as well. – AwkwardCoder Jun 01 '11 at 16:18
  • @AwkwardCoder - Can't say I have. Why do you ask? – keyboardP Jun 01 '11 at 16:19