1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-25 03:42:48 +01:00

rwengine: Pass CharacterController to the CharacterObject constructor

The CharacterObject should remove the CharacterController upon
destruction.

Should fix this memory leak:
==31441== 480 (360 direct, 120 indirect) bytes in 5 blocks are definitely lost in loss record 2,038 of 2,723
==31441==    at 0x4C2F1CA: operator new(unsigned long) (vg_replace_malloc.c:334)
==31441==    by 0x7E2370: GameWorld::createPedestrian(unsigned short, glm::tvec3<float, (glm::precision)0> const&, glm::tquat<float, (glm::precision)0> const&, unsigned int) (GameWorld.cpp:334)
==31441==    by 0x8E2F01: TrafficDirector::populateNearby(ViewCamera const&, float, int) (TrafficDirector.cpp:164)
==31441==    by 0x7E1170: GameWorld::createTraffic(ViewCamera const&) (GameWorld.cpp:176)
==31441==    by 0x75C616: RWGame::tick(float) (RWGame.cpp:531)
==31441==    by 0x75BF18: RWGame::run() (RWGame.cpp:420)
==31441==    by 0x749184: main (main.cpp:15)
This commit is contained in:
Anonymous Maarten 2017-09-13 03:05:42 +02:00 committed by Daniel Evans
parent c2da40d0a0
commit 8e98cf2311
9 changed files with 29 additions and 25 deletions

View File

@ -11,15 +11,14 @@
constexpr float kCloseDoorIdleTime = 2.f;
CharacterController::CharacterController(CharacterObject *character)
: character(character)
CharacterController::CharacterController()
: character(nullptr)
, _currentActivity(nullptr)
, _nextActivity(nullptr)
, m_closeDoorTimer(0.f)
, currentGoal(None)
, leader(nullptr)
, targetNode(nullptr) {
character->controller = this;
}
bool CharacterController::updateActivity() {

View File

@ -75,7 +75,7 @@ protected:
AIGraphNode* targetNode;
public:
CharacterController(CharacterObject* character);
CharacterController();
virtual ~CharacterController() {
}
@ -139,6 +139,8 @@ public:
CharacterObject* getTargetCharacter() const {
return leader;
}
friend class CharacterObject;
};
#define DECL_ACTIVITY(activity_name) \

View File

@ -9,8 +9,8 @@ class DefaultAIController : public CharacterController {
glm::vec3 gotoPos;
public:
DefaultAIController(CharacterObject* character)
: CharacterController(character) {
DefaultAIController()
: CharacterController() {
}
glm::vec3 getTargetPosition();

View File

@ -5,8 +5,8 @@
#include <objects/CharacterObject.hpp>
#include <objects/VehicleObject.hpp>
PlayerController::PlayerController(CharacterObject* character)
: CharacterController(character)
PlayerController::PlayerController()
: CharacterController()
, lastRotation(glm::vec3(0.f, 0.f, 0.f))
, _enabled(true) {
}

View File

@ -13,7 +13,7 @@ class PlayerController : public CharacterController {
bool _enabled;
public:
PlayerController(CharacterObject* character);
PlayerController();
/**
* @brief Enables and disables player input.

View File

@ -331,9 +331,9 @@ CharacterObject* GameWorld::createPedestrian(const uint16_t id,
data->loadModel(id);
}
auto ped = new CharacterObject(this, pos, rot, pt);
auto controller = new DefaultAIController();
auto ped = new CharacterObject(this, pos, rot, pt, controller);
ped->setGameObjectID(gid);
new DefaultAIController(ped);
pedestrianPool.insert(ped);
allObjects.push_back(ped);
return ped;
@ -358,10 +358,11 @@ CharacterObject* GameWorld::createPlayer(const glm::vec3& pos,
pt->setModel(model);
}
auto ped = new CharacterObject(this, pos, rot, pt);
auto controller = new PlayerController();
auto ped = new CharacterObject(this, pos, rot, pt, controller);
ped->setGameObjectID(gid);
ped->setLifetime(GameObject::PlayerLifetime);
players.push_back(new PlayerController(ped));
players.push_back(controller);
pedestrianPool.insert(ped);
allObjects.push_back(ped);
return ped;

View File

@ -17,7 +17,8 @@
const float CharacterObject::DefaultJumpSpeed = 2.f;
CharacterObject::CharacterObject(GameWorld* engine, const glm::vec3& pos,
const glm::quat& rot, BaseModelInfo* modelinfo)
const glm::quat& rot, BaseModelInfo* modelinfo,
CharacterController* controller)
: GameObject(engine, pos, rot, modelinfo)
, currentState({})
, currentVehicle(nullptr)
@ -31,7 +32,7 @@ CharacterObject::CharacterObject(GameWorld* engine, const glm::vec3& pos,
, physCharacter(nullptr)
, physObject(nullptr)
, physShape(nullptr)
, controller(nullptr) {
, controller(controller) {
auto info = getModelInfo<PedModelInfo>();
setClump(ClumpPtr(info->getModel()->clone()));
@ -43,6 +44,8 @@ CharacterObject::CharacterObject(GameWorld* engine, const glm::vec3& pos,
}
animations = engine->data->getAnimGroup(info->animgroup_);
controller->character = this;
}
CharacterObject::~CharacterObject() {
@ -50,6 +53,7 @@ CharacterObject::~CharacterObject() {
if (currentVehicle) {
currentVehicle->setOccupant(getCurrentSeat(), nullptr);
}
delete controller;
}
void CharacterObject::createActor(const glm::vec2& size) {

View File

@ -81,7 +81,8 @@ public:
* @param ped PEDS_t struct to use.
*/
CharacterObject(GameWorld* engine, const glm::vec3& pos,
const glm::quat& rot, BaseModelInfo* modelinfo);
const glm::quat& rot, BaseModelInfo* modelinfo,
CharacterController* controller);
~CharacterObject();

View File

@ -12,10 +12,10 @@ BOOST_AUTO_TEST_CASE(test_create) {
{
auto character =
Global::get().e->createPedestrian(1, {100.f, 100.f, 50.f});
BOOST_REQUIRE(character != nullptr);
auto controller = new DefaultAIController(character);
auto controller = character->controller;
BOOST_REQUIRE(controller != nullptr);
// Check the initial activity is Idle.
BOOST_CHECK_EQUAL(controller->getCurrentActivity(), nullptr);
@ -36,10 +36,10 @@ BOOST_AUTO_TEST_CASE(test_activities) {
{
auto character =
Global::get().e->createPedestrian(1, {0.f, 0.f, 225.6f});
BOOST_REQUIRE(character != nullptr);
auto controller = new DefaultAIController(character);
auto controller = character->controller;
BOOST_REQUIRE(controller != nullptr);
controller->setNextActivity(
new Activities::GoTo(glm::vec3{10.f, 10.f, 0.f}));
@ -56,7 +56,6 @@ BOOST_AUTO_TEST_CASE(test_activities) {
glm::distance(character->getPosition(), {10.f, 10.f, 0.f}), 0.1f);
Global::get().e->destroyObject(character);
delete controller;
}
{
VehicleObject* vehicle = Global::get().e->createVehicle(
@ -65,10 +64,10 @@ BOOST_AUTO_TEST_CASE(test_activities) {
BOOST_REQUIRE(vehicle->getModel() != nullptr);
auto character = Global::get().e->createPedestrian(1, {0.f, 0.f, 0.f});
BOOST_REQUIRE(character != nullptr);
auto controller = new DefaultAIController(character);
auto controller = character->controller;
BOOST_REQUIRE(controller != nullptr);
controller->setNextActivity(new Activities::EnterVehicle(vehicle, 0));
@ -123,7 +122,6 @@ BOOST_AUTO_TEST_CASE(test_death) {
auto character =
Global::get().e->createPedestrian(1, {100.f, 100.f, 50.f});
BOOST_REQUIRE(character != nullptr);
auto controller = new DefaultAIController(character);
BOOST_CHECK_EQUAL(character->getCurrentState().health, 100.f);
BOOST_CHECK(character->isAlive());
@ -144,7 +142,6 @@ BOOST_AUTO_TEST_CASE(test_death) {
character->animations->animation(AnimCycle::KnockOutShotFront0));
Global::get().e->destroyObject(character);
delete controller;
}
}