Solved

Object Disposed Exception

Posted on 2014-04-02
7
306 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 74

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 74

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now