Skip to content

Commit

Permalink
PlatformPlayer: Deallocate synth and receiver explicitly.
Browse files Browse the repository at this point in the history
We'll have memory leaks otherwise because Java won't free them up
even if the sequencer that was using them is closed. Related to #10.
  • Loading branch information
AShiningRay committed Mar 3, 2025
1 parent a6cc9a1 commit 3d83962
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/org/recompile/mobile/PlatformPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,18 +430,7 @@ public midiPlayer() // For when a Locator call (usually for tones) is issued

public midiPlayer(InputStream stream)
{
try
{
synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
if(Mobile.useCustomMidi)
{
synthesizer.loadAllInstruments(Manager.customSoundfont);
}
receiver = synthesizer.getReceiver();
midi = MidiSystem.getSequencer(false);
midiSequence = MidiSystem.getSequence(stream);
}
try { midiSequence = MidiSystem.getSequence(stream); }
catch (Exception e)
{
Mobile.log(Mobile.LOG_ERROR, PlatformPlayer.class.getPackage().getName() + "." + PlatformPlayer.class.getSimpleName() + ": " + "Couldn't load MIDI file: " + e.getMessage());
Expand All @@ -452,6 +441,13 @@ public void realize()
{
try
{
synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
if(Mobile.useCustomMidi)
{
synthesizer.loadAllInstruments(Manager.customSoundfont);
}
receiver = synthesizer.getReceiver();
midi = MidiSystem.getSequencer(false);
midi.getTransmitter().setReceiver(receiver);
midi.open();
Expand Down Expand Up @@ -526,6 +522,8 @@ public void deallocate()
midi.removeMetaEventListener(metaListener);
metaListener = null;
}
synthesizer.close();
receiver = null;
midi.close();
}

Expand All @@ -536,6 +534,8 @@ public void close()
midi.removeMetaEventListener(metaListener);
metaListener = null;
}
synthesizer.close();
receiver = null;
midi.close();
midiSequence = null;
}
Expand Down

0 comments on commit 3d83962

Please sign in to comment.