Link to home
Start Free TrialLog in
Avatar of Dodsworth
Dodsworth

asked on

Object Disposed Exception

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?
Avatar of kaufmed
kaufmed
Flag of United States of America image

It would help to see the code in question.
Avatar of Dodsworth
Dodsworth

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thought that might be the case. So I need to copy the e.Imagestream to mainStream somehow?
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.
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.
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 :)