Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi.

I got randomly Object disposed exception in my application 1 out of 5 times if I ran the program. It does not throw exceptions every time i ran the program.

stacktrace says the exception is occured at the line where file1 = nothing.

How do i solve this erratick behaviour ?

Better way to implemt the code?
VB
Dim sbSQL As New StringBuilder
sbSQL.Append("SELECT Item_Id from MyTableA")
Dim ds1 As New DataSet
Using daFindOutdated As New SqlDataAdapter(sbSQL.ToString, cn)
    daFindOutdated.Fill(ds1)
End Using

Dim sbPath As New StringBuilder
For Each row As DataRow In ds1.Tables(0).Rows
    sbPath.Append(ConfigurationManager.AppSettings("ZipFilePath") & "TBL" & row.Item("Item_ID").ToString.PadLeft(5, "0") & ".zip")
    If File.Exists(sbPath.ToString) Then
        Dim zip As New ZipArchive(New DiskFile(sbPath.ToString))
        Dim folder1 As AbstractFolder = zip.GetFolder("\Item\File\")
        Dim file1 As AbstractFile = folder1.GetFile("Tbl.cpf")

        Using sw1 As New StreamWriter(file1.OpenWrite(True))
            sw1.AutoFlush = True
            sw1.WriteLine("")
            sw1.Close()
            file1 = Nothing
        End Using

        Dim folder2 As AbstractFolder = zip.GetFolder("\Item\File\")
        Dim file2 As AbstractFile = folder2.GetFile("Tbl.cmp")

        Using sw2 As New StreamWriter(file2.OpenWrite(True))
            sw2.AutoFlush = True
            sw2.WriteLine("")
            sw2.Close()
            file2 = Nothing
        End Using

        Dim folder3 As AbstractFolder = zip.GetFolder("\Item\File\")
        Dim file3 As AbstractFile = folder3.GetFile("Tbl.upc")

        Using sw3 As New StreamWriter(file3.OpenWrite(True))
            sw3.AutoFlush = True
            sw3.WriteLine("")
            sw3.Close()
            file3 = Nothing
        End Using

        zip = Nothing
    End If

    sbPath.Length = 0
Next


What I have tried:

I tried to use System.Threading.Thread.Sleep(1000) before it assigns nothing to file1 or zip object. But I do not think this is good idea or it will or it will not help. There is more likely the program iterates more than 10000 times to create these three different zip files. Please help me out. Thank you.
Posted
Updated 15-May-17 4:00am
v3
Comments
Ralf Meier 15-May-17 3:10am    
OK ...
- Why do you assign Nothing to File1,2,3 ? I think it is a relict from VB6 ...
- What does your Debugger says ? I don't htink that the Error really comes by the described way ... I would first look what happens with file1 when it is assigned (if it is assigned)
[no name] 15-May-17 7:50am    
As I have said, I need to iterate over more than 10000times to create zip file for each item in db. For example for item1, there will be TBL00001.zip folder which will have Tbl.cmp, tbl.cpf and tbl.upc files. Stacktrace shows disposed object exception at the line where file1=nothing. This does not happen everytime. It happens once in every 5/6 times when I ran the program. It's hard to debug 10000 x3 times to get the point where program throw exception as its does not happen all the time.
Ralf Meier 15-May-17 8:25am    
I'm sorry - I don't read that.
I have the following suggestions :
- set AutoFlush to False
- build a Try-Catch around each Using-Block which could help you to debug
- try to reduce the count of files - as more files do you have in one Folder as slower becomes your System with reading and writing ...
F-ES Sitecore 15-May-17 10:17am    
remove the "file1 = nothing" lines, and if file1 has a Dispose or Close method call it after End Using instead.
[no name] 15-May-17 10:33am    
I am not familiar with vb.net as well. This is old code, i didnt write it. I am assuming here , code is calling file1 =nothing, so that when code it goes back(on loop) it will create new file1 obj for new file . For eg. TBL00002zip\item\file\tbl.cmp.

1 solution

Why do you write
VB
sw1.Close()
file1 = Nothing


I am not familiar with visual basic but using operator (calling Dispose() of StreamWritter) should close and dispose the file1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900