Memory full when loading multiple images - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Memory full when loading multiple images (/showthread.php?tid=3191) |
Memory full when loading multiple images - Ikerkaz - 11-02-2024 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 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 Anybody can help me? Thank you !!! RE: Memory full when loading multiple images - bplus - 11-02-2024 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. RE: Memory full when loading multiple images - Ikerkaz - 11-02-2024 (11-02-2024, 11:25 PM)bplus Wrote: To reuse same imagehandle& number: 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? RE: Memory full when loading multiple images - bplus - 11-03-2024 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. RE: Memory full when loading multiple images - SMcNeill - 11-03-2024 (11-02-2024, 11:32 PM)Ikerkaz Wrote:(11-02-2024, 11:25 PM)bplus Wrote: To reuse same imagehandle& number: 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 RE: Memory full when loading multiple images - SMcNeill - 11-03-2024 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 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. RE: Memory full when loading multiple images - Ikerkaz - 11-03-2024 (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") 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.My code is too large... I wouldn't know what piece to share in order you can help me Anyway, thank you both for your help RE: Memory full when loading multiple images - bplus - 11-03-2024 Good! |