Solved

Object Disposed Exception

Posted on 2014-04-02
7
317 Views
Last Modified: 2014-04-02
I'm getting this error...

System.ObjectDisposedException: Cannot access a closed Stream.     at System.IO.__Error.StreamIsClosed()     at System.IO.MemoryStream.Seek(Int64 offset, SeekOrigin loc)

I haven't disposed of the stream in question.. so who has ?

Are streams cleaned up differently ? The stream in question is defined at class level.

Any ideas?
0
Comment
Question by:Dodsworth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39972194
It would help to see the code in question.
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39972309
I've chopped the code down so it's easier to read, which has prompted a thought..
When I dispose of e.ImageStream is mainStream also being disposed because I'm using '=' ?

Partial Public Class Camera

    Private mainStream As Stream    'This is where I'm holding the image stream from the camera
    Private saveNow As Boolean = True   'Save the image straight away or on click of save button

    Private Sub cam_CaptureImageAvailable(ByVal sender As Object, ByVal e As Microsoft.Devices.ContentReadyEventArgs)
        Try
            mainStream = e.ImageStream
            If saveNow Then
                saveImage() 'This call does not error
            End If
        Catch err As Exception
            Stop
        Finally
            e.ImageStream.Close()
        End Try
    End Sub

    Private Sub saveImage()
        mainStream.Seek(0, SeekOrigin.Begin)
        library.SavePictureToCameraRoll(Date.UtcNow.Ticks.ToString & ".jpg", mainStream)
    End Sub

    Private Sub Ok_Click(sender As Object, e As EventArgs)
        saveImage() 'Here Errors :(
    End Sub
End Class

Open in new window

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39972428
Basically the stream is being disposed by the line:
e.ImageStream.Close()

Open in new window

At which point your mainStream variable now holds a reference to a disposed object.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 1

Author Comment

by:Dodsworth
ID: 39972461
Thought that might be the case. So I need to copy the e.Imagestream to mainStream somehow?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39972480
Or remove the line:
e.ImageStream.Close()

Open in new window

From your Finally clause, and then close the stream when you are done with it (probably in your SaveImage() method.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39972587
So I need to copy the e.Imagestream to mainStream somehow?
You can use the Clone method for this purpose.

and then close the stream when you are done with it (probably in your SaveImage() method.
But you'll also want to include some checking on the state of that variable, in case someone clicks the button twice. I'd suggest setting the variable to Nothing after you close the image, and then add a check for Is Nothing in your save code.
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39972722
It would seem that e.ImageStream goes out of scope outside cam_CaptureImageAvailable.

I tried
e.ImageStream.CopyTo(mainStream)

Open in new window

but got a ArgumentNullException which the documentation indicates that is because the destination is Nothing.  
Private mainStream As New Stream

Open in new window

Resulted in
'New' cannot be used on a class that is declared 'MustInhert'
So I changed it to
Private mainStream As New MemoryStream

Open in new window

And all is well :)
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Code enhancement 4 33
VB.NET Inline If statement 4 41
VB.net Excel VSTO Add-in version compatability 2 17
T-SQL and CLR parameter strings 9 27
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question