Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
graceful way to test for a valid image handle such as before doing _FreeImage?
#1
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:

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
Reply


Messages In This Thread
graceful way to test for a valid image handle such as before doing _FreeImage? - by madscijr - 11-10-2025, 08:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Test to post a new Thread + question 32 or 64 bit Rudy M 2 538 09-09-2025, 04:10 PM
Last Post: Rudy M
  A strange experience with forum posting an image TempodiBasic 2 521 06-17-2025, 06:01 AM
Last Post: TempodiBasic
  QB64PE programming challenge? auto-convert image to photoreal etch-a-sketch drawing madscijr 9 1,805 02-14-2025, 05:49 PM
Last Post: madscijr
  Got a slow PC? Test these changes, kindly! SMcNeill 7 1,710 05-16-2024, 09:59 PM
Last Post: grymmjack
  When to free an image? James D Jarvis 5 1,127 04-16-2023, 08:55 PM
Last Post: OldMoses

Forum Jump:


Users browsing this thread: 1 Guest(s)