![]() |
|
graceful way to test for a valid image handle such as before doing _FreeImage? - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2) +--- Thread: graceful way to test for a valid image handle such as before doing _FreeImage? (/showthread.php?tid=4096) |
graceful way to test for a valid image handle such as before doing _FreeImage? - madscijr - 11-10-2025 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 LongExample 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 pathRE: graceful way to test for a valid image handle such as before doing _FreeImage? - DSMan195276 - 11-10-2025 (11-10-2025, 08:58 PM)madscijr Wrote: It leads me to wonder: is there a way to determine if an image handle is valid, other than error trapping?No because it is never safe to do that. Image handles get reused, it's impossible for the code you described to tell the difference between when the original image hasn't been free'd yet or when the image handle _was_ free'd and has since been reused by a completely unrelated `_NewImage` call. This issue also means your `ON ERROR` approach is not safe either, for the same reason - sometimes your second `_FreeImage` call _will_ work, but will be freeing a completely unrelated image you created with `_NewImage` somewhere else in the code. You simply _have_ to properly track whether an image has been free'd and then avoid doing anything with it after that point. Ex. When you do the first `_FreeImage`, set `image1` to 0, that way the old handle is no longer in the variable when you go to check it again and the stale handle is no longer there to be free'd by mistake. RE: graceful way to test for a valid image handle such as before doing _FreeImage? - SMcNeill - 11-11-2025 Code: (Select All)
Just write your own freeimage routine and have it *always* set to handle back to 0 when it's freed the handle. If a handle isn't loaded at all, then it's going to be 0. If a handle is > 0 then it's going to be a screen handle and not needing to be freed. Only LOAD when handles are 0 or -1, and only FREE when the handle is < -1 and then set the handle back to 0. It's about the easiest way to deal with the issue that I know of. RE: graceful way to test for a valid image handle such as before doing _FreeImage? - madscijr - 11-11-2025 Thanks for answering! While I think it's kind of silly that QB64/PE doesn't provide some way to track & test whether a variable is currently associated with a valid image, it's not THAT big a deal, and I'll just keep in mind to make sure to keep track of that. QB64 compiles to C, so I'm not that surprised we have to do some of that manually, the trade-off being that because our programs compile to C, they can be FAST! Thanks again guys RE: graceful way to test for a valid image handle such as before doing _FreeImage? - SMcNeill - 11-11-2025 I tend to go with 0 simply as it's an uninitialized variable value and also what we consider in most things to be FALSE. (And anyone trying to _FreeImage 0 is *really* doing something wrong to begin with. )
RE: graceful way to test for a valid image handle such as before doing _FreeImage? - madscijr - 11-11-2025 Either -1 or 0 works for me - great idea, nice and simple. Thanks! RE: graceful way to test for a valid image handle such as before doing _FreeImage? - SMcNeill - 11-11-2025 -1 just means it wasn't initialized properly. For example you try to _LOADIMAGE and spell the name or path wrong, so the file can't be found and can't load. It doesn't mean it ever existed or was freed properly. If you need that information, store it as something uniquely obscure like using a SINGLE to hold the value -0.5, or whatnot. RE: graceful way to test for a valid image handle such as before doing _FreeImage? - madscijr - 11-11-2025 (11-11-2025, 03:34 PM)SMcNeill Wrote: -1 just means it wasn't initialized properly. For example you try to _LOADIMAGE and spell the name or path wrong, so the file can't be found and can't load. It doesn't mean it ever existed or was freed properly. If you need that information, store it as something uniquely obscure like using a SINGLE to hold the value -0.5, or whatnot.Uniquely obscure, I like it! |