0
\$\begingroup\$

Whenever I try to manually call the dispose method in one of my screen my game just crashes.

My dispose looks something like this

@Override
public void dispose() {
    battletheme.dispose();
    renderer.dispose();
    bg.dispose();
    enemy.dispose();
    dialouge.dispose();
    GameTools.fontText.dispose();
}

and I use

this.dispose();

When I switch to another screen, otherwise my memory begins to pile up. Is there any reason for this?

EDIT: My spritebatch called "renderer" seems to crash on dispose, any reason?

\$\endgroup\$
  • \$\begingroup\$ Can you show the exception thrown when it crashes? \$\endgroup\$ – bornander Dec 7 '15 at 20:39
  • 1
    \$\begingroup\$ The crash is probably because you used the function to call itself, with infinite recursion. You're probably looking for super.dispose() \$\endgroup\$ – Draco18s no longer trusts SE Dec 7 '15 at 20:58
1
\$\begingroup\$

The dispose() function is made to be called at the end of the program's life cycle. You calling renderer.dispose() is manually garbage-collecting the renderer and this most likely works by setting it to null which will, in turn, cause the JVM to respond by destroying the object. You are probably trying to re-access the renderer object after it's destruction and failing by causing a NullPointerException which is produced when a variable contains a null value due to the fact that you can't use null to do anything as null means nothing :)

Hope this helps! ~ Ammar Tarajia

\$\endgroup\$

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.