user4959397
user4959397

Reputation:

LibGDX: AssetsManager GdxRuntimeException when disposing it

Hello I have a problem with the AssetsManager from libGDX.

I load a TTF and a Skin with a AssetsManager and add the TTF to the Skin. Then when I dispose the AssetsManager it throws a exception.

I know the problem should be that the skin and asssets manager do dispose the same pixmap. But is there a way to prevent this to happen?

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Pixmap already disposed!
at com.badlogic.gdx.graphics.Pixmap.dispose(Pixmap.java:315)
at com.badlogic.gdx.graphics.g2d.PixmapPacker$Page$1.dispose(PixmapPacker.java:384)

Code:

    FreeTypeFontLoaderParameter ttfParam = new FreeTypeFontLoaderParameter();
    ttfParam.fontFileName = Const.TTF_ARIAL_PATH;

    if (!manager.isLoaded(Const.TTF_ARIAL_SMALL, BitmapFont.class)) {
        ttfParam.fontParameters.size = 16;

        manager.load(Const.TTF_ARIAL_SMALL, BitmapFont.class, ttfParam);
    }

    if (!manager.isLoaded(Const.TTF_ARIAL_NORMAL, BitmapFont.class)) {
        ttfParam.fontParameters.size = 32;

        manager.load(Const.TTF_ARIAL_NORMAL, BitmapFont.class, ttfParam);
    }

    if (!manager.isLoaded(Const.TTF_ARIAL_LARGE, BitmapFont.class)) {
        ttfParam.fontParameters.size = 64;

        manager.load(Const.TTF_ARIAL_LARGE, BitmapFont.class, ttfParam);
    }

    if (!manager.isLoaded(Const.EDITOR_UI_SKIN, Skin.class)) {
        SkinParameter skinParam = new SkinParameter(Const.EDITOR_UI_SKIN_PATH);
        manager.load(Const.EDITOR_UI_SKIN, Skin.class, skinParam);
    }

    manager.finishLoading();

    skin = manager.get(Const.EDITOR_UI_SKIN, Skin.class);

    BitmapFont font = manager.get(Const.TTF_ARIAL_SMALL, BitmapFont.class);
    skin.add(Const.TTF_ARIAL_SMALL, font, BitmapFont.class);

EDIT: I solved it. But I don't like the solution.

public void dispose() {
    skin.remove(Const.TTF_ARIAL_SMALL, BitmapFont.class);
    manager.dispose();
}

Upvotes: 3

Views: 166

Answers (1)

Mh_Bash
Mh_Bash

Reputation: 36

Once the asset is not needed anymore and you want to free it to avoid memory leaks. use :

manager.unload( yourAsset ) ;

It is interesting to point out if you want to free all assets, no matter whether queued or loaded, at once, instead of doing it one by one:

manager.clear() ;

Nevertheless, by using the clear( ) method, AssetManager is still alive, finally:

manager.dispose

Upvotes: 1

Related Questions