0
\$\begingroup\$

can someone tell me how to close game and dispose objects properly? I tried several methods but always have problems. I have ImageButton with ClickListener.

Here's code of listener:

exitListener = new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                getGame().dispose(); // reference to GameClass 
                Gdx.app.exit();
                super.clicked(event, x, y);
            }
        };

but something causes problems because I have error:

buffer not allocated with newUnsafeByteBuffer or already disposed

and logs show that stage.dispose() causes problems.

\$\endgroup\$
1
\$\begingroup\$

The problem is because you're trying to dispose of the Stage while you're still inside the clicklistener, and it isn't done processing yet. Instead of trying to quit at that point, I would set a variable like stillRunning = false; in your listener, and then check it at the end of your game loop in render() when you know your process is outside of the Stage.

\$\endgroup\$
3
  • \$\begingroup\$ I changed my ClickListener by removing getGame().dispose(); and when Im click on button I get logs that all my screens etc. were removed so It looks like everything works but it's ok? \$\endgroup\$
    – Land93
    Nov 23 '16 at 22:56
  • \$\begingroup\$ Not disposing of your resources would be more like what @Thomas is suggesting in his answer. Admittedly, OS memory management isn't my forte, but I would advise you look into what non-disposal will mean on different platforms (such as Android). There's an interesting SO thread here on the subject: stackoverflow.com/questions/24363419/… \$\endgroup\$
    – John
    Nov 24 '16 at 0:50
  • \$\begingroup\$ Im dospising all resources in dispose() method but only when the game automatically call dispose() \$\endgroup\$
    – Land93
    Nov 24 '16 at 15:51
1
\$\begingroup\$

Some people argue that the proper way to dispose of your game properly is by letting the Operating System do it. That means, don't free any resources at all, just quit the program and let the OS do the clean up. Of course, that only goes for resources that the OS is defined to clean up, and also of course, not everybody agrees with this mentality.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ As usual: downvoters, please leave comments as well, so we can know what's wrong with this answer. \$\endgroup\$
    – Thomas
    Nov 23 '16 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.