Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
[kernel][event bus] Fixing NPE due to loose of the reference to Inter…
Browse files Browse the repository at this point in the history
…nalEventBusSKill.

The AgentEventListener internal class of InternalEventBusSkill looses
the weak reference to its enclosing object. The AgentEventListener class
is no more static to have this reference always set.

see #100.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Feb 8, 2015
1 parent ff73059 commit 2a45f6a
Showing 1 changed file with 16 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import io.sarl.lang.core.EventListener;
import io.sarl.lang.core.Skill;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
Expand Down Expand Up @@ -92,7 +91,7 @@ class InternalEventBusSkill extends Skill implements InternalEventBusCapacity {
*/
public InternalEventBusSkill(Agent agent, Address addressInInnerDefaultSpace) {
super(agent);
this.agentAsEventListener = new AgentEventListener(this);
this.agentAsEventListener = new AgentEventListener();
this.agentAddressInInnerDefaultSpace = addressInInnerDefaultSpace;
}

Expand Down Expand Up @@ -191,20 +190,17 @@ public final EventListener asEventListener() {
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
private static class AgentEventListener implements EventListener {
private class AgentEventListener implements EventListener {

private Queue<Event> buffer = Queues.newConcurrentLinkedQueue();

private final WeakReference<InternalEventBusSkill> skill;

private final UUID aid;

private boolean isKilled;

@SuppressWarnings("synthetic-access")
public AgentEventListener(InternalEventBusSkill skill) {
this.skill = new WeakReference<>(skill);
this.aid = skill.getOwner().getID();
public AgentEventListener() {
this.aid = InternalEventBusSkill.this.getOwner().getID();
}

@Override
Expand All @@ -219,31 +215,30 @@ public void receiveEvent(Event event) {
&& (!(event instanceof Destroy))
&& (!(event instanceof AsynchronousAgentKillingEvent)))
: "Unsupported type of event: " + event; //$NON-NLS-1$
InternalEventBusSkill s = this.skill.get();
synchronized (s) {
synchronized (InternalEventBusSkill.this) {
if (event instanceof AgentSpawned
&& this.aid.equals(((AgentSpawned) event).agentID)) {
&& this.aid.equals(((AgentSpawned) event).agentID)) {
// This permits to ensure that the killing event
// is correctly treated when fired from the initialization
// handler.
fireEnqueuedEvents(s);
fireEnqueuedEvents(InternalEventBusSkill.this);
if (this.isKilled) {
killOwner(s);
killOwner(InternalEventBusSkill.this);
return;
}
}
switch(s.state.get()) {
switch(InternalEventBusSkill.this.state.get()) {
case NEW:
this.buffer.add(event);
break;
case RUNNING:
fireEnqueuedEvents(s);
s.eventBus.post(event);
fireEnqueuedEvents(InternalEventBusSkill.this);
InternalEventBusSkill.this.eventBus.post(event);
break;
case DESTROYED:
// Dropping messages since agent is dying
s.logger.debug(InternalEventBusSkill.class,
"EVENT_DROP_WARNING", event); //$NON-NLS-1$
InternalEventBusSkill.this.logger.debug(InternalEventBusSkill.class,
"EVENT_DROP_WARNING", event); //$NON-NLS-1$
break;
default:
throw new IllegalStateException();
Expand Down Expand Up @@ -274,11 +269,10 @@ private void killOwner(InternalEventBusSkill s) {

@SuppressWarnings("synthetic-access")
void killOrMarkAsKilled() {
InternalEventBusSkill s = this.skill.get();
synchronized (s) {
synchronized (InternalEventBusSkill.this) {
this.isKilled = true;
if (s.state.get() != OwnerState.NEW) {
killOwner(s);
if (InternalEventBusSkill.this.state.get() != OwnerState.NEW) {
killOwner(InternalEventBusSkill.this);
}
}
}
Expand Down

0 comments on commit 2a45f6a

Please sign in to comment.