When to free an image? - 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: When to free an image? (/showthread.php?tid=1621) |
When to free an image? - James D Jarvis - 04-13-2023 When is it a good idea to use _freeimage to clean up an image from memory? The obvious answer is of course... when you are done with it. But is it necessary at programs end to clean up the image handles if they were declared in the main program or only in subroutines? Code: (Select All) dim shared image1 as long RE: When to free an image? - TerryRitchie - 04-13-2023 This is just my opinion however I feel freeing images (and sounds) from memory before a program terminates is a good programming practice to get in the habit of. It's obviously necessary before exiting subs and functions that are not STATIC or the images/sounds are not SHARED. Is it necessary in the main body before program termination? I'm not sure if QB64's compiler takes garbage collection into account like this. RE: When to free an image? - SMcNeill - 04-13-2023 A whole lot of it depends on usage. handle = _LOADIMAGE... DO 'Stuff LOOP END With the above, you really don't have to worry about FreeImage. load it once, use it for the whole program, then it'll free itself at program termination. Honestly though, I'd probably still free it -- even if it isn't necessary before the END statement-- just to stay in good practice and never let lazy habits lead to issues elsewhere in the future. DO handle = LOADIMAGE.. 'Stuff LOOP END Now with the above here, you're going to have problems. You're loading images repeatedly in the DO LOOP, and you're never freeing them. You, my friend, now have a massive memory leak and all the issues that arises from such! Main module, inside a SUB, FUNCTION, or GOSUB... The rule of thumb is simple -- when you're done with the image, just get in the habit of always freeing it. RE: When to free an image? - James D Jarvis - 04-13-2023 (04-13-2023, 05:02 PM)SMcNeill Wrote: ..good to know. What happens here? ---------------------- DO handle= _NEWIMAGE... handle = LOADIMAGE.. 'Stuff LOOP END --------------------------- RE: When to free an image? - mnrvovrfc - 04-13-2023 More trouble. RE: When to free an image? - OldMoses - 04-16-2023 Since image handles are variables, I tend to treat images like their handle variables, some are global and some are SUB/FUNCTION local. The local ones, created at each routine call, are always freed before leaving the subroutine so that they don't consume more and more memory via multiple future routine calls. Global image handles are generally used often during the program so I leave them in memory, unless I'm sure I'm done with them. This is of course an oversimplification, highly dependent on the nature of what's going on in the program, but that's my typical approach. Often, when coming into a routine that throws up temporary images such a dialog/input boxes, the very first thing I'll do is: backimg& = _COPYIMAGE(0) Then I: localimg& = _{NEW/LOAD}IMAGE(x, y, mode) 'do stuff with localimg& and finally: _FREEIMAGE localimg& Then before I leave the routine: _PUTIMAGE, backimg& _FREEIMAGE backimg& That assumes no alteration of the main screens display during the call. |