[Request] Image loading without memory management

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
Akabane87
Posts: 50
Joined: Sat May 05, 2012 6:11 pm

[Request] Image loading without memory management

Post by Akabane87 »

Hi there,

not sure it is really the right place here to do this but I think it is an annoying problem that could be easily fixed.

Irrlicht is using full memory management with image texture loading, meaning that you are not supposed to care about allocation and deallocation of your textures. The problem is that if you really need to manage yourself the memory and delete a texture at a given moment it becomes really painfull to do this.

For example suppose you use a spritebank to manage a list of icons for a listbox. You want to flush it completely : remove all textures definitly and generate new ones. You will face a big memory increse if you simply clear the spritebank, because the reference count of your cleared sprites will be 1 because they are stored in the engine. Now suppose your new list is filled with big images and a large amount of it, and you flush it for the 4th time : you get a crash on a new and suddenly realize that your app is greater than 1.9GB in the ram... Hell !

And the only way I found to do what I want to do is to do something painful like this :

Code: Select all

 
// remove textures from the engine because we want to delete them now !
for(u32 i = 0; i < bank->getTextureCount(); ++i)
{
    Texture* tex = bank->getTexture(i);
    driver->removeTexture(tex);
}
bank->clear();
 
Since I had to do similar things in my overall programm, I finally made those terrible macro to get full control of my textures :

Code: Select all

 
#define SAFE_LOAD_IMAGE(driver, image, path, usePremultipliedAlpha) if(!image) \
{ \
    image = driver->getTexture(path); \
    if(image) \
    { \
        image->grab(); \
        driver->removeTexture(image); \
        if(usePremultipliedAlpha) \
        PremultiplyAlpha(image); \
    } \
} \

#define SAFE_UNLOAD(res) if(res) \
{ \
    res->drop(); \
    res = NULL; \
} \
So my suggestion is, why not adding an optionnal flag in getTexture() to tell the engine wether or not we want irrlicht to store and control the texture ?
CuteAlien
Admin
Posts: 9634
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [Request] Image loading without memory management

Post by CuteAlien »

Huh -why did you use a macro? Just write a function for this. But yeah - I was wondering about that in the past. Thought it should not be a flag but a function called createTexture as it returns a pointer which the user has to drop (all such functions in Irrlicht always start with the word create).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Akabane87
Posts: 50
Joined: Sat May 05, 2012 6:11 pm

Re: [Request] Image loading without memory management

Post by Akabane87 »

It could be too :p as long as the engine doesn't keep a reference on it the form used doen't matter. About the macro there is absolutely no reason ^^ except the one that if was only 2 lines at the beginning and now it is a bit more :p (and I'm too lazy to replace it now). Let's say it spare my cpu 1 call/ret instruction lol.
Post Reply