help dispose of image

talahaski

Active member
Joined
Apr 29, 2004
Messages
35
Can somebody please help me dispose of a image, I want to delete the image afterwards and the OS is holding it.

simple code I thought would work, but does not.

dim imagetoload as string="c:/temp.jpg"
Dim img As Image
try
img.FromFile(ImageToLoad)
catch
messagebox.show("Image is corrupt")
end try
img.Dispose()
If optx = 1 Then
Copy(ImageToLoad, NewFileName)
else
File.Delete(ImageToLoad)
end if


This blows up at the img.dispose() statement.
 
I think the problem might be that I have other lock issues too. I definitly have some major resource leaks. I dont have the code with me right now so it will have to wait until tonight before I can post more info.

Are there any good free tools that can check your source code for resource leaks?
 
ok, here is the error

At run time I get the following error


An unhandled exception of type system.nullReferenceException occurred

Additional info - object reference not set to an instance of an object


Any ideas?
 
Heres the full source

Please help with resource leaks and this image.dispose error.


Private Sub ImportPhoto(ByVal ImageToLoad As String)
Dim MaxImageNumber As Integer = 0
Dim NewFileName As String
Dim CheckDupResult As Integer = 0
Dim img As Image
img.FromFile(ImageToLoad)
MaxImageNumber = GetMaxImageNumber()
MaxImageNumber = MaxImageNumber + 1
NewFileName = PhotosetPath + CStr(MaxImageNumber) + GetExtension(ImageToLoad)
img.Dispose()
If ImportCheckForDuplicate = 1 Then
CheckDupResult = CheckDuplicate(ImageToLoad)
End If
If CheckDupResult = 0 Then
Try
Copy(ImageToLoad, NewFileName)
If ImportDeleteSource = 1 Then
Try
File.Delete(ImageToLoad)
Catch
MessageBox.Show("error deleting source file")
End Try
End If
Catch
MessageBox.Show("Error copying image")
errors = 1
End Try
End If
End Sub


Private Function GetMaxImageNumber() As Integer
Dim filestr As String
Dim Max As Integer = 0
If Directory.Exists(PhotosetPath) Then
For Each filestr In Directory.GetFiles(PhotosetPath)
If IsNumeric(Path.GetFileNameWithoutExtension(filestr)) Then
If CInt(Path.GetFileNameWithoutExtension(filestr)) > Max Then
Max = Path.GetFileNameWithoutExtension(filestr)
End If
End If
Next
End If
Return Max
End Function


Private Function CheckDuplicate(ByVal PrimaryFile As String) As Integer
Returns 0 if no duplicates,
returns 1 if duplicate found
Dim filestr As String
Dim rtn As Integer = 0
Dim Hash As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim PrimaryHashBytes As Byte() = Hash.ComputeHash(New FileStream(PrimaryFile, FileMode.Open, FileAccess.Read))
Try
If Directory.Exists(PhotosetPath) Then
For Each filestr In Directory.GetFiles(PhotosetPath)
Dim Hash2 As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim CompareHashBytes As Byte() = Hash2.ComputeHash(New FileStream(filestr, FileMode.Open, FileAccess.Read))
Try
If System.Text.ASCIIEncoding.ASCII.GetString(PrimaryHashBytes) = System.Text.ASCIIEncoding.ASCII.GetString(CompareHashBytes) Then
Duplicatefile1 = PrimaryFile
Duplicatefile2 = filestr
Duplicate = 0
frmIShowDuplicate.ShowDialog()
If Duplicate = 1 Then
rtn = 1
End If
End If
Catch
MessageBox.Show("Error ASCIIEncoding")
End Try
Hash2.Clear()
Next
End If
Catch
MessageBox.Show("Error Directory does not Exist")
End Try
Hash.Clear()
Return rtn
End Function
 
darn.


The dispose is working great, but now my delete statement is NOT working.

File.Delete(ImageToLoad)


Something else must be holding a lock on this file.
 
Yah, Im going through it one function at a time checking the OS to see when it locks the file.

It looks like its coming from the function CheckDuplicate now.

Something about the hashing that I must be leaving open.
 
I thought the hash.clear() was cleaning up everything, but it looks like its not.


Any idea on how to clean up after these?

Dim Hash As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim PrimaryHashBytes As Byte() = Hash.ComputeHash(New FileStream(PrimaryFile, FileMode.Open, FileAccess.Read))
 
Ok,

I got this fixed.

Thanks for all the help everybody.

I had to change around the checkduplicate function, added some closes and set=nothing

Here is the completed function



Private Function CheckDuplicate(ByVal PrimaryFile As String) As Integer
Returns 0 if no duplicates,
returns 1 if duplicate found
Dim filestr As String
Dim rtn As Integer = 0
Dim fs1 As FileStream
Dim fs2 As FileStream
Dim fs2Open As Integer = 0

fs1 = New FileStream(PrimaryFile, FileMode.Open, FileAccess.Read)

Dim Hash As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim PrimaryHashBytes As Byte() = Hash.ComputeHash(fs1)
Dim CompareHashBytes As Byte()
Try
If Directory.Exists(PhotosetPath) Then
For Each filestr In Directory.GetFiles(PhotosetPath)
Check if file ext are the same, if not skip compare
If GetExtension(filestr) = GetExtension(PrimaryFile) Then
fs2open = 1
Dim Hash2 As New System.Security.Cryptography.MD5CryptoServiceProvider
fs2 = New FileStream(filestr, FileMode.Open, FileAccess.Read)
CompareHashBytes = Hash2.ComputeHash(fs2) New FileStream(filestr, FileMode.Open, FileAccess.Read))

Try
If System.Text.ASCIIEncoding.ASCII.GetString(PrimaryHashBytes) = System.Text.ASCIIEncoding.ASCII.GetString(CompareHashBytes) Then
Duplicatefile1 = PrimaryFile
Duplicatefile2 = filestr
Duplicate = 0
frmIShowDuplicate.ShowDialog()
If Duplicate = 1 Then
rtn = 1
End If
End If
Catch
MessageBox.Show("Error ASCIIEncoding")
End Try
CompareHashBytes = Nothing
Hash2.Clear()
End If
Next
End If
Catch
MessageBox.Show("Error Directory does not Exist")
End Try
PrimaryHashBytes = Nothing
Hash.Clear()
fs1.Close()
If fs2Open = 1 Then
fs2.Close()
End If
Return rtn
End Function
 
Back
Top