PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Deleting Loaded Images

Is there a way to delete images that you've loaded with the "Frog::LoadImage(display, FilePath)" function?

I'm not too familiar with the inner workings of pocketfrog, but it appears that it created a new surface with
Code:
Surface* pSurface = new Surface( this, new Internal::MemoryGraphicBuffer( width, height ) );


So, does that mean I need to delete it before loading another in the case of programming a slideshow application?

An example:
Code:

for(int i=0; i<5; i++)
{
MyImage = Frog::LoadImage(display, FilePath[i]);
display->blit...
//Delete MyImage?
}
Yes, to delete an image, simply... delete it =)

Code:

MyImage = Frog::LoadImage(display, FilePath[i]);
// display->blit...
delete MyImage;
Or better yet, load all the images first (preferably in GameInit() or whatever and delete them on exit (i.e. GameEnd()).

Code:

bool MyGame::GameInit()
{
...
for (int i=0; i<4; i++)
m_imageArray[i] = LoadImage(display, FilePath[i]);
...
}
void MyGame::GameLoop()
{
...
for (int i=0; i<5; i++)
display->Blit(x,y,m_imageArray[i]);
...
}
void MyGame::GameEnd()
{
...
for (int i=0; i<5; i++)
delete m_imageArray[i];
...
}

That way, you don't have to allocate and deallocate memory during the game loop. Loading images will take some time and not something you want to be doing EVERY LOOP.

Anwyays.. if you take a look at the Blit sample in the PocketFrog samples dir this is exactly what's been done.

Normally, you can spot memory leaks by matching every call to "new" with a call to "delete" (or delete[] where applicable), but you have to watch out for those factory functions that allocate memory for you. LoadImage is one of those.

Cheers!
fastrx wrote:So, does that mean I need to delete it before loading another in the case of programming a slideshow application?


No, you don't NEED to, but it is strongly advised since otherwise you will have a memory leak on your hands.
One thing to keep in mind is that with the current version of PF, it is using imgdecmp.dll to load images, which has a substantial memory overhead. If you have a fair number of images, loading them all up-front will cause the memory requirements of your game to grow in a hurry.

Look no further than my Invasion: Trivia for a prime example of this.

My current game project is using GapiDraw, but even if I wasn't I'd be doing what I am, which is to load the images needed for each screen a they are required, I pop up a 'Loading...' screen. It turns out it takes a fraction of a second per screen, so it's not a problem.

The way I do it, in case any is actually interested, is to branch out of the main game loop to the screen that I am loading. When I'm there, I branch while loading and do small bits of the loads each frame, then call the code for the 'Loading...' screen.

Basically, I simply have a switch block where each case does a few loads, then increments a counter that is the variable checked against in the switch. My default clause simply flips a switch to tell me loading is done, and then the actual screen code begins.

Then, when the screen is done, I basically do the same thing but do all my deletes at that point. I pop the 'Loading...' screen again, even though it's not loading, and the next screen in line goes right into the loading state, so it's seemless.

I like this approach rather than spawning a separate thread because I don't have to deal with any of the issues that come along with threads, it basically works within the framework provided by the library.

This has actually worked out exceptionally well, it's a suggestion I think others may want to consider. Note also that my game's load time is maybe two seconds now, as opposed to 30+ seconds for Invasion: Trivia on a current device.
Well obviously, as FZammetti pointed out above, should you never load a lot of images if you are not going to use them.. Hence, loading only the ones needed for the current stage is the way to go.

I think I would have spawned a thread to load all the images, but unless you are very familiar with threads FZammettis way seems to be much easier.
fzammetti wrote:
One thing to keep in mind is that with the current version of PF, it is using imgdecmp.dll to load images, which has a substantial memory overhead.


I don't think the overhead is that big... Sure there is an unnecessary overhead, but it's not like it's using megs of memory. Loading an image of 320x240 would require 307200 bytes for the 32 bits DC and 153600 bytes for the 16 bits DC. That's a total of 460800 bytes (or 450K).

fzammetti wrote:
If you have a fair number of images, loading them all up-front will cause the memory requirements of your game to grow in a hurry.


450K is hardly a reason to write home. And that's for a 320x240 image mind you. And this is not cumulative, as both DCs are destroyed before loading the next image. Although SBL's comments about the memory overhead are true, I have to wonder how big of a deal it is in practice.

What kind of images are you loading that would require so much memory?

Where I could see a problem though, is the way PF rotate the images to match the display. But this is a speed issue, not a memory one. Rest assured that this is going away in the next PF.
I just did a quick run-through of the resources for Invasion: Trivia...

There are 133 WAVs, uncompressed, total size of 2,974,477 bytes.

There are 208 images, stored as 256-color palettized and optimized PNG's, total UNCOMPRESSED size of 4,321,078 (to calculate this, I brought them into Paint Shop Pro, started the optimizer, and recorded the uncompressed value it read).

The breakdown of images is:

3 x 150x22
4 x 16x16
27 x 240x320
1 x 459x320
1 x 402x340
24 x 70x70
24 x 106x120
4 x 10x48
12 x 144x30
4 x 128x80
6 x 240x30
4 x 32x32
3 x 140x207
3 x 76x280
6 x 68x56
9 x 71x88
7 x 87x74
4 x 24x24
12 x 48x48
4 x 150x58
2 x 149x59

The balance of the images are a mix of sizes, all unique, none greater than 98x73 (average of 52x49).

As for other resources, they total 123,121 bytes, all uncompressed.

Data structures account for another approx. 19,000 bytes. The rest is code, which accounts for 213,384 bytes (calculated by simply removing all resoures and recording the compiled executable's size.

In other words, I can account for an approximate memory requirement of 7,651,060. However, anything less than 14M and the game either doesn't load (99% of the time) or loads but crashes later on. If that much memory is free though, it loads and runs without problem.

Strangely, the number coincides with what if might be it the image memory was doubled, a little higher than that even, that's why I've always thought (ever since reading it in the GD docs anyway) that the image loaded might be the issue.
fzammetti wrote:
Strangely, the number coincides with what if might be it the image memory was doubled, a little higher than that even, that's why I've always thought (ever since reading it in the GD docs anyway) that the image loaded might be the issue.


Well I don't believe in that coincidence. This means that there is a memory leak in the PF image loading code that nobody has noticed or reported before.

Are your PNGs stored in the resources section of your executable or as separate files? Have you tried to other way around?

Is there an easy way for you to make sure you are not loading the images twice? (I don't mean to insult you, I just don't have access to your code to double check it...)
They are stored as resources. I did at one point try them as files, I thought that maybe since the entire executable was loaded into memory, then the images decompressed that that was the cause, but it didn't seem to make a difference.

Don't worry about insulting me, it usually turns out to be another stupid programmer trick, and I wouldn't be surprised to find I did something boneheaded this time too :) In this case however, I'm resonably sure they aren't loading twice. How I could prove it I'm not certain, but knowing how the code is organized I don't believe it to be the case.

If you want, I'd be happy to send the code your way it you think it would help. I was planning on open-sourcing the game in a few months anyway, but since it's selling like a roting corpse, doing it sooner doesn't really bother me :(
fzammetti wrote:
They are stored as resources. I did at one point try them as files, I thought that maybe since the entire executable was loaded into memory, then the images decompressed that that was the cause, but it didn't seem to make a difference.


I recall having read somewhere that on WinCE (and hence PocketPC) image resources arn't loaded into memory together with the rest of the resources. They are read from disk when needed to save precious memory on these "tiny" devices =)
Well, if that's the case (and it certainly makes sense), than that's one less possible reason. Thanks for the info!
adde":2g45l58n]


Maybe the PPC does not treat PNG files as "image resources" but insted as common files stored on the resource table and then loading them to memory together with the surfaces that fzammetti is using. Duplicating them to memory...
A good thing to try is to call some api that tells the free memory and use it on some critical loading parts on the game, to see how much memory is consumed by each part and/or by some images
I go for loading everything up front ( but I also hold with global variables ). My game loads mainly jpg & a few bitmaps (bitmaps when I need black to be black for transparency) and everything loads up in a few seconds. Most graphics are meshes loaded in PGL's format. Most time is lost in universe creation but even so the new game startup time is pretty quick. I have not looked at exe size but will have a check to see what I'm using.
One thing I learned in the process of doing I:T (after the fact actually, so it didn't help for that game) is that loading large images is faster than loading a number of smaller images that equals the larger. In other words, it's better speed-wise to have a big 1024x768 image containing a bunch of smaller images comprising animations and such and blitting them with source rectangles than storing each frame separately. I'm not sure of memory utilization implications, but it does improve load times.
As for a related question, if you have several hunder different small (3x3) surfaces that have to load a single PNG graphic, what's the best way to pull that off? As far as I can tell, repeated loading just keeps on loading the same PNG and decoding it... is there a better way?
I haven't done any benchmarks, but since loading the images requires a callout to imgdecmp.dll, I wouldn't re-load it for sure. My thinking would be to use Surface::Clone, I'd bet on that being faster. Another way is to blit the one image you load onto the other surfaces, but without looking at the code I would bet that's how Thierry implemented Clone anyway.
Yeah, thanks.... using Clone() changed my loading time from about 10 seconds to less than a third of a second.

I checked the source and yes, Clone() does blit.
Why would you want to load the same image more then once? Sounds like a waste of memory/time to me. What usage do you have of these hundreads of images?
It was for my pathetic particle demo thing; see the "I was bored" thread a couple of threads below.
After some research, it seems that resources are the cullprint, not imgdecmp or PF.

PF will use LoadResource() / LockResource() to access the image data. This basically commit some memory to the process that is never freed until the game ends. It seems there is no way to "unload" a resource. FreeResource() exists on the desktop PC, but isn't there on the PPC.

I will double check all this. But if it is indeed the case, then using images as resources is a bad idea on the PPC. That is just too bad.
Well, I'm simultaneously happy and sad... Happy because you've solved the problem, sad because I really didn't want to externalize my resources. It's so much easier to work with them when they are resources (IMHO). But I'm glad to hear your on to something. When your completely convinced that's the issue, let me know and I'll drop all the resources out to files and see how it goes. I know I tried this at some point, I guess maybe I didn't do it with all the resources and the problem didn't really go away with enough of them still in there.