Skip to content

Commit

Permalink
fixing memory leak and evaluation order #12 #13
Browse files Browse the repository at this point in the history
  • Loading branch information
behrisch committed Jan 25, 2024
1 parent 1493964 commit 2617528
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
25 changes: 13 additions & 12 deletions src/microsim/transportables/MSPModel_JuPedSim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,21 @@ MSPModel_JuPedSim::add(MSTransportable* person, MSStageMoving* stage, SUMOTime n
const Position arrivalPosition = arrivalLane->getShape().positionAtOffset(stage->getArrivalPos());
waypoints.push_back(arrivalPosition);

JPS_JourneyDescription journey = JPS_JourneyDescription_Create();
JPS_JourneyDescription journeyDesc = JPS_JourneyDescription_Create();
JPS_StageId startingStage = 0;
JPS_StageId predecessor = 0;
for (const Position& p : waypoints) {
if (!addWaypoint(journey, predecessor, p, person->getID())) {
JPS_JourneyDescription_Free(journey);
if (!addWaypoint(journeyDesc, predecessor, p, person->getID())) {
JPS_JourneyDescription_Free(journeyDesc);
return nullptr;
}
if (startingStage == 0) {
startingStage = predecessor;
}
}
JPS_ErrorMessage message = nullptr;
JPS_JourneyId journeyId = JPS_Simulation_AddJourney(myJPSSimulation, journey, &message);
JPS_JourneyId journeyId = JPS_Simulation_AddJourney(myJPSSimulation, journeyDesc, &message);
JPS_JourneyDescription_Free(journeyDesc);
if (message != nullptr) {
WRITE_WARNINGF(TL("Error while adding a journey for person '%': %"), person->getID(), JPS_ErrorMessage_GetMessage(message));
JPS_ErrorMessage_Free(message);
Expand All @@ -216,15 +217,15 @@ MSPModel_JuPedSim::add(MSTransportable* person, MSStageMoving* stage, SUMOTime n
}
}
if (state == nullptr) {
state = new PState(static_cast<MSPerson*>(person), stage, journey, journeyId, startingStage, waypoints);
state = new PState(static_cast<MSPerson*>(person), stage, journeyId, startingStage, waypoints);
state->setLanePosition(stage->getDepartPos());
state->setPreviousPosition(departurePosition);
state->setPosition(departurePosition.x(), departurePosition.y());
state->setAngle(departureLane->getShape().rotationAtOffset(stage->getDepartPos()));
myPedestrianStates.push_back(state);
myNumActivePedestrians++;
} else {
state->reinit(stage, journey, journeyId, startingStage, waypoints);
state->reinit(stage, journeyId, startingStage, waypoints);
}
if (state->isWaitingToEnter()) {
tryPedestrianInsertion(state, state->getPosition(*state->getStage(), now));
Expand Down Expand Up @@ -270,7 +271,9 @@ MSPModel_JuPedSim::execute(SUMOTime time) {

if (state->isWaitingToEnter()) {
// insertion failed at first try so we retry with some noise
Position p = state->getPosition(*state->getStage(), time) + Position(RandHelper::rand(-.5, .5), RandHelper::rand(-.5, .5));
Position p = state->getPosition(*state->getStage(), time);
p.setx(p.x() + RandHelper::rand(-.5, .5)); // we do this separately to avoid evaluation order problems
p.sety(p.y() + RandHelper::rand(-.5, .5));
tryPedestrianInsertion(state, p);
++stateIt;
continue;
Expand Down Expand Up @@ -829,29 +832,27 @@ MSLane* MSPModel_JuPedSim::getNextPedestrianLane(const MSLane* const currentLane
// MSPModel_Remote::PState method definitions
// ===========================================================================
MSPModel_JuPedSim::PState::PState(MSPerson* person, MSStageMoving* stage,
JPS_JourneyDescription journey, JPS_JourneyId journeyId, JPS_StageId stageId,
JPS_JourneyId journeyId, JPS_StageId stageId,
const PositionVector& waypoints)
: myPerson(person), myStage(stage), myJourney(journey), myJourneyId(journeyId), myStageId(stageId), myWaypoints(waypoints),
: myPerson(person), myStage(stage), myJourneyId(journeyId), myStageId(stageId), myWaypoints(waypoints),
myAgentId(0), myPosition(0, 0), myAngle(0), myWaitingToEnter(true) {
}


void
MSPModel_JuPedSim::PState::reinit(MSStageMoving* stage, JPS_JourneyDescription journey, JPS_JourneyId journeyId, JPS_StageId stageId,
MSPModel_JuPedSim::PState::reinit(MSStageMoving* stage, JPS_JourneyId journeyId, JPS_StageId stageId,
const PositionVector& waypoints) {
if (myStage != nullptr) {
myStage->setPState(nullptr); // we need to remove the old state reference to avoid double deletion
}
myStage = stage;
myJourney = journey;
myJourneyId = journeyId;
myStageId = stageId;
myWaypoints = waypoints;
}


MSPModel_JuPedSim::PState::~PState() {
JPS_JourneyDescription_Free(myJourney);
}


Expand Down
6 changes: 2 additions & 4 deletions src/microsim/transportables/MSPModel_JuPedSim.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class MSPModel_JuPedSim : public MSPModel {
*/
class PState : public MSTransportableStateAdapter {
public:
PState(MSPerson* person, MSStageMoving* stage, JPS_JourneyDescription journey, JPS_JourneyId journeyId, JPS_StageId stageId, const PositionVector& waypoints);
PState(MSPerson* person, MSStageMoving* stage, JPS_JourneyId journeyId, JPS_StageId stageId, const PositionVector& waypoints);
~PState() override;

void reinit(MSStageMoving* stage, JPS_JourneyDescription journey, JPS_JourneyId journeyId, JPS_StageId stageId, const PositionVector& waypoints);
void reinit(MSStageMoving* stage, JPS_JourneyId journeyId, JPS_StageId stageId, const PositionVector& waypoints);

Position getPosition(const MSStageMoving& stage, SUMOTime now) const override;
void setPosition(double x, double y);
Expand Down Expand Up @@ -134,8 +134,6 @@ class MSPModel_JuPedSim : public MSPModel {
private:
MSPerson* myPerson;
MSStageMoving* myStage;
/// @brief handle to the JPS journey, only needed for freeing the memory later
JPS_JourneyDescription myJourney;
/// @brief id of the journey, needed for modifying it
JPS_JourneyId myJourneyId;
JPS_StageId myStageId;
Expand Down

0 comments on commit 2617528

Please sign in to comment.