On Wed, Mar 27, 2013 at 4:07 PM, Luís de Sousa <[log in to unmask]> wrote:

> I noted today that geometries that have been removed using this method
> continue to be drawn in the display. I have an Agent class that
> inherits from MasonGeometry; this particular agent can "die" during
> simulation and its stop() method is invoked. When this happens with an
> agent stored in a Continuous2D, for instance, invoking the remove()
> method is enough to prevent it from being drawn again.
>
> After invoking removeGeometry() is there anything else referencing the geometry?
>

More than likely you are not un-scheduling the agent, so the MASON schedule has the lone reference to your agent.  I just modified the CampusWorld demo to have a single agent that's deleted after 200 steps, and that appears to work ok.

Essentially, in CampusWorld.java I added a Stoppable, used that when adding the single agent, and then added a new function, removeAgent(), to unschedule same and remove it from its field:


    Stoppable agentStoppable; // Used to unschedule agent

    /**
     * Add agents to the simulation and to the agent GeomVectorField. Note that
     * each agent does not have any attributes.
     */
    void addAgents()
    {
        for (int i = 0; i < numAgents; i++)
        {
            Agent a = new Agent(this);

            agents.addGeometry(a.getGeometry());

            // Remember the Stoppable here
            agentStoppable = schedule.scheduleRepeating(a);
        }
    }


    void removeAgent()
    {
        // first remove the agent from the scheduler
        agentStoppable.stop();

        // then remove it from the field; there's only one agent, so just
        // remove the first guy
        agents.removeGeometry((MasonGeometry) agents.getGeometries().get(0));
    }


Then to put all the parts in motion I added this to start():


         // After 200 time steps remove the one agent
        schedule.scheduleOnce(200, new Steppable() {
            public void step(SimState ss)
            {
                removeAgent();
            }
        });


Sure enough, after 200 steps the lone agent pops out of existence.