Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.97a is out! (02/02/24); New blog post: Simulator Enhancements (03/13/24)

Author Topic: removeListenerOfClass() causes a ConcurrentModificationException  (Read 619 times)

LazyWizard

  • Global Moderator
  • Admiral
  • *****
  • Posts: 1363
    • View Profile
    • GitHub Profile

I remember there being a similar problem in the past with other removeX methods being called by the removed object (since they'd be doing so during iteration of the list they're contained in). However, for some reason this exception occurs even if I offload the removal onto a separate EveryFrameScript:

Code
runcode if ($context.isCampaignAccessible())
{
$sector.addTransientScript(new EveryFrameScript()
{
private boolean isDone = false;
public void advance(float amount)
{
$sector.getListenerManager().removeListenerOfClass(org.lazywizard.console.ConsoleCampaignListener.class);
isDone = true;
$print("Console disabled in the campaign until the next reload.\n");
}
public boolean isDone() { return isDone; }
public boolean runWhilePaused() { return true; }
});
}

Quote
226895 [Thread-4] ERROR com.fs.starfarer.combat.CombatMain  - java.util.ConcurrentModificationException
java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
        at java.util.ArrayList$Itr.next(ArrayList.java:859)
        at com.fs.starfarer.campaign.ListenerManager.removeListenerOfClass(Unknown Source)
        at SC$1.advance(Unknown Source)
        at com.fs.starfarer.campaign.CampaignEngine.advance(Unknown Source)
        at com.fs.starfarer.campaign.CampaignState.advance(Unknown Source)
        at com.fs.starfarer.BaseGameState.traverse(Unknown Source)
        at com.fs.state.AppDriver.begin(Unknown Source)
        at com.fs.starfarer.combat.CombatMain.main(Unknown Source)
        at com.fs.starfarer.StarfarerLauncher$1.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 23988
    • View Profile
Re: removeListenerOfClass() causes a ConcurrentModificationException
« Reply #1 on: January 18, 2020, 12:57:47 PM »

Hmm. At a glance, not seeing why this might be, though it's possible I tweaked something here - ah yes, looking at the SVN history, I did.

Baaasically in the current release, the removeListenerOfClass method is fundamentally borked and can't succeed, period. The only workaround would be to use the removeListener method after finding the specific object you want to remove by iterating over the result of getListeners(). The method is fixed for the next release.
Logged