Solved

Object Disposed Exception

Posted on 2014-04-02
7
315 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

792 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