1
Answer

ObjectDisposedException

Nepethya Rana

Nepethya Rana

7y
265
1
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?  
 
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
 
Answers (1)
0
Sujeet Singh
NA 129 0 7y
this is usually means one of two things:
  1. There is some very unusual code path which, when taken, always causes a crash
  2. A race condition exists between threads in your program. Thus, the crash appears unpredictably and may be difficult to reproduce

My advice to you is to start the program in the debugger, and leave it running until the exception is thrown. Then you can come back here and provide us with the relevant code, stack trace, debug output, etc.