Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When to free an image?
#1
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
dim shared image2 as long
screen _newimage (640,400,32)
_fullscreen

image1= _LoadImage("Data\im_1.png") 

_putimage (0,0)-(639,399),image1
LOADTWO
_putimage (0,0)-(339,3199),image2
'should either of these two lines be used (without being commented out)
'_freeimgae image1
'_freeimage image2
end

sub loadtwo
image1= _LoadImage("Data\im_2.png")
end sub
Reply
#2
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.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#3
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.
Reply
#4
(04-13-2023, 05:02 PM)SMcNeill Wrote: ..
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!
good to know.

What happens here? 
----------------------
DO
   handle= _NEWIMAGE...
   handle = LOADIMAGE..
   'Stuff
LOOP
END
---------------------------
Reply
#5
More trouble.
Reply
#6
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.
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply




Users browsing this thread: 5 Guest(s)