Solved

Object Disposed Exception

Posted on 2014-04-02
7
319 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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