This problem came up earlier, where I did a _FreeImage on an image that had already been freed.
It leads me to wonder: is there a way to determine if an image handle is valid, other than error trapping?
Example 1: error when doing _FreeImage on an invalid image handle:
Example 2: one way to handle the error, kind of kludgey but works:
It leads me to wonder: is there a way to determine if an image handle is valid, other than error trapping?
Example 1: error when doing _FreeImage on an invalid image handle:
Code: (Select All)
Dim image1 As Long
Dim MaxRow As Integer
Screen _NewImage(640, 480, 32)
Cls , _RGB32(0, 0, 255)
MaxRow = _Height / _FontHeight
image1 = _NewImage(100, 100, 32)
_Dest image1
Cls , _RGB32(255, 0, 0)
_PutImage (0, 0), image1, 0
_Dest 0
Color _RGB32(0, 0, 0), _RGB32(255, 255, 0)
Locate MaxRow - 3, 1: Print "Press any key";
Sleep
Screen 0
If image1 < -1 Or image1 > 0 Then _FreeImage image1
' The next line will fail with an Invalid Handle error:
If image1 < -1 Or image1 > 0 Then _FreeImage image1
' QUESTION: How can we gracefully test that image1 is a valid image before doing a _FreeImage?Example 2: one way to handle the error, kind of kludgey but works:
Code: (Select All)
Dim Shared m_ProgramPath As String: m_ProgramPath = Left$(Command$(0), _InStrRev(Command$(0), "\")) ' executable path
Dim Shared m_ProgramName As String: m_ProgramName = Mid$(Command$(0), _InStrRev(Command$(0), "\") + 1) ' executable filename
Dim Shared ErrNum As Long
Dim Shared ErrLine As Long
Main
End
FreeImageErrorHandler:
ErrNum = Err
ErrLine = _ErrorLine
Resume Next
Sub Main
Dim image1 As Long
Dim MaxRow As Integer
Screen _NewImage(640, 480, 32)
Cls , _RGB32(0, 0, 255)
MaxRow = _Height / _FontHeight
image1 = _NewImage(100, 100, 32)
_Dest image1
Cls , _RGB32(255, 0, 0)
_PutImage (0, 0), image1, 0
_Dest 0
Color _RGB32(0, 0, 0), _RGB32(255, 255, 0)
Locate MaxRow - 3, 1: Print "Press any key";
Sleep
Screen 0
FreeImage image1
' The next line would cause an Invalid Handle error without error handling code
FreeImage image1
' QUESTION: Do we just have to do On Error Goto or
' is there a way to gracefully test that image1 is a valid image
' before doing a _FreeImage?
Cls
Print "Handled error gracefully"
End Sub ' Main
Sub FreeImage (MyImage As Long)
ErrNum = 0
On Error GoTo FreeImageErrorHandler
If MyImage < -1 Or MyImage > 0 Then _FreeImage MyImage
On Error GoTo 0
' Ignore invalid handle error but quit if something else
If ErrNum <> 0 Then
If ErrNum <> 258 Then
_MessageBox m_ProgramName, "Error " + _ToStr$(ErrNum) + " at line " + _ToStr$(ErrLine), "error"
System
End If
End If
End Sub ' FreeImage

)