Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Memory full when loading multiple images
#1
Question 
Hi to all !!!

I will try to explain myself with my limited english ?

I am making a space shooting/strategy game that loads multiple images. I have a main image (ship) and when it is attacked I load two different images in other two layers (shields impact, damages made to the ship). Last thing I do is _PUTIMAGE of these three layers. During the game I use _LOADIMAGE to change the images of these layers, and there are several ships in the game with the same three layers.

I noticed that when I start my game, the Windows task manager shows some RAM used, and as I am playing it this RAM is increasing.

I know the _FREEIMAGE command, but my code is very large and I find very difficult to check when to use it...

Also I noticed that when I execute a _LOADIMAGE command, and some time later I execute this command over the same variable the memory is still increasing. I thought that when using the same variable the RAM occupied was the same... obviously I was wrong Sad

Is there any way to re-use some LONG variable to load several images without having to _FREEIMAGE between every load? I thought to make a SUB that makes a _FREEIMAGE in the variable just before load an image, but it goes wrong a lot of times Sad

Anybody can help me? Thank you !!! Smile
10 PRINT "Hola! Smile"
20 GOTO 10
Reply
#2
To reuse same imagehandle& number:

If imagehandle& < -1 then ' it contains an image
_freeimage imagehandle&
end if

imagehandle& = _LoadImage( ) ' load imagehandle& for reuse

Just do the first thing before each time you do the 2nd thing.
b = b + ...
Reply
#3
(11-02-2024, 11:25 PM)bplus Wrote: To reuse same imagehandle& number:

If imagehandle& < -1 then ' it contains an image
_freeimage imagehandle&
end if

imagehandle& = _LoadImage( ) ' load imagehandle& for reuse

Just do the first thing before each time you do the 2nd thing.

Thank you for your reply. But the wiki says in _FREEIMAGE " Note that calling _FREEIMAGE only frees the handle. It does NOT reset the variable used to store the handle back to 0."
I tried an example loading an image, its handle was -9 (for example), and after calling _FREEIMAGE its handle was -9... how can I know that I have freed the image? Maybe resetting manually the variable to 0?
10 PRINT "Hola! Smile"
20 GOTO 10
Reply
#4
Doesn't matter what the number is, you need to clear memory of old image to reuse number for a new image.

Why don't you just try the one line of code in your program where needed and see if that doesn't fix things?
Code: (Select All)
If imagehandle& < -1 then _freeimage imagehandle&

BTW I am starting to wonder if you are wasting allot of time reloading the same images into your program when you should be just switching imagehandles between loaded images.
b = b + ...
Reply
#5
(11-02-2024, 11:32 PM)Ikerkaz Wrote:
(11-02-2024, 11:25 PM)bplus Wrote: To reuse same imagehandle& number:

If imagehandle& < -1 then ' it contains an image
_freeimage imagehandle&
end if

imagehandle& = _LoadImage( ) ' load imagehandle& for reuse

Just do the first thing before each time you do the 2nd thing.

Thank you for your reply. But the wiki says in _FREEIMAGE " Note that calling _FREEIMAGE only frees the handle. It does NOT reset the variable used to store the handle back to 0."
I tried an example loading an image, its handle was -9 (for example), and after calling _FREEIMAGE its handle was -9... how can I know that I have freed the image? Maybe resetting manually the variable to 0?

If you need to reset that variable, you can do as bplus is suggesting and add this:

Code: (Select All)
If imagehandle& < -1 then _freeimage imagehandle&: imagehandle& = 0 'reset the handle to 0 after you free it
Reply
#6
There's just one glaring problem with what bplus wrote, which might affect you, and that's if you're calling _LOADIMAGE inside a sub.

Code: (Select All)
SUB Foo
    temp = _LOADIMAGE("whatever.png",32)
    ...  stuff
END SUB

It won't matter if you add that style IF before that _LOADIMAGE statement, as temp is *always* going to be 0 and you're *always* going to be loading images without freeing them.

You need to _FREEIMAGE your stuff once it goes out of scope and isn't being used any more.

OR

You need to simply load your images ONCE when the program starts up and initializes, and then use and resuse those same image handles over and over inside your program where they're needed.



Without sharing the code, there's no way anyone can tell you where to place a _FREEIMAGE statement.   Share what you have, and then we can help point out what might be the best solution for your problems, so you can fix them and move forward without your memory leaking.  Wink
Reply
#7
(11-03-2024, 12:22 AM)bplus Wrote: BTW I am starting to wonder if you are wasting allot of time reloading the same images into your program when you should be just switching imagehandles between loaded images.
Can I switch handles, and the image "inside" the variable also changes? I thought it was impossible.
Something like that:

Code: (Select All)
Img1=_LOADIMAGE("sun.png")
Img2=_LOADIMAGE("moon.png")
Dest=Img1 ' Now Dest is the picture of the sun
Dest=Img2 ' Now it is the moon

EDIT: I made a quick program with only 10 images loading and "speed switching" between them, and it seems that it works!!! I will try this with my game. THANK YOU VERY MUCH !!!


(11-03-2024, 02:49 AM)SMcNeill Wrote: Without sharing the code, there's no way anyone can tell you where to place a _FREEIMAGE statement.   Share what you have, and then we can help point out what might be the best solution for your problems, so you can fix them and move forward without your memory leaking.  Wink
My code is too large... I wouldn't know what piece to share in order you can help me Sad


Anyway, thank you both for your help Wink
10 PRINT "Hola! Smile"
20 GOTO 10
Reply
#8
Good!
b = b + ...
Reply




Users browsing this thread: 3 Guest(s)