1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 15:02:34 +02:00

rwlib: Use ClumpPtr instead of Clump*

Should fix these memory leaks:
==22737== 14,598,040 (131,472 direct, 14,466,568 indirect) bytes in 2,739 blocks are definitely lost in loss record 3,124 of 3,126
==22737==    at 0x4C2F1CA: operator new(unsigned long) (vg_replace_malloc.c:334)
==22737==    by 0x90FE4B: LoaderDFF::loadFromMemory(std::shared_ptr<FileContentsInfo>) (LoaderDFF.cpp:443)
==22737==    by 0x7BCC86: GameData::loadModel(unsigned short) (GameData.cpp:474)
==22737==    by 0x7DF7BC: GameWorld::createInstance(unsigned short, glm::tvec3<float, (glm::precision)0> const&, glm::tquat<float, (glm::precision)0> const&) (GameWorld.cpp:144)
==22737==    by 0x7DF44C: GameWorld::placeItems(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (GameWorld.cpp:120)
==22737==    by 0x758D38: RWGame::newGame() (RWGame.cpp:116)
==22737==    by 0x786389: LoadingState::enter() (LoadingState.cpp:9)
==22737==    by 0x75DC59: void StateManager::enter<LoadingState, RWGame*, RWGame::RWGame(Logger&, int, char**)::{lambda()#1}>(RWGame*&&, RWGame::RWGame(Logger&, int, char**)::{lambda()#1}&&) (StateManager.hpp:40)
==22737==    by 0x758484: RWGame::RWGame(Logger&, int, char**) (RWGame.cpp:81)
==22737==    by 0x747815: main (main.cpp:13)
This commit is contained in:
Anonymous Maarten 2017-09-13 00:47:22 +02:00 committed by Daniel Evans
parent bc8652baba
commit 7e4e1db85d
25 changed files with 85 additions and 77 deletions

View File

@ -135,14 +135,14 @@ public:
}
/// @todo change with librw
void setAtomic(Clump* model, int n, AtomicPtr atomic) {
void setAtomic(ClumpPtr model, int n, AtomicPtr atomic) {
model_ = model;
/// @todo disassociated the Atomic from Clump
atomics_[n] = atomic;
}
/// @todo remove this
Clump* getModel() const {
ClumpPtr getModel() const {
return model_;
}
@ -178,11 +178,10 @@ public:
}
bool isLoaded() const override {
return model_ != nullptr;
return model_.get() != nullptr;
}
void unload() override {
delete model_;
model_ = nullptr;
}
@ -249,7 +248,7 @@ public:
}
private:
Clump* model_ = nullptr;
ClumpPtr model_;
std::array<AtomicPtr, 3> atomics_;
float loddistances_[3] = {};
uint8_t numatomics_ = 0;
@ -280,25 +279,24 @@ public:
ClumpModelInfo(ModelDataType type) : BaseModelInfo(type) {
}
void setModel(Clump* model) {
void setModel(ClumpPtr model) {
model_ = model;
}
Clump* getModel() const {
ClumpPtr getModel() const {
return model_;
}
bool isLoaded() const override {
return model_ != nullptr;
return model_.get() != nullptr;
}
void unload() override {
delete model_;
model_ = nullptr;
}
private:
Clump* model_ = nullptr;
ClumpPtr model_ = nullptr;
};
/**

View File

@ -4,7 +4,7 @@
#include <loaders/LoaderDFF.hpp>
#include <queue>
Animator::Animator(Clump* model) : model(model) {
Animator::Animator(ClumpPtr model) : model(model) {
}
void Animator::tick(float dt) {

View File

@ -8,7 +8,7 @@
#include <map>
#include <rw/defines.hpp>
class Clump;
#include <rw/forward.hpp>
class ModelFrame;
/**
@ -40,7 +40,7 @@ class Animator {
/**
* @brief model The model being animated.
*/
Clump* model;
ClumpPtr model;
/**
* @brief Currently playing animations
@ -48,7 +48,7 @@ class Animator {
std::vector<AnimationState> animations;
public:
Animator(Clump* model);
Animator(ClumpPtr model);
Animation* getAnimation(unsigned int slot) {
if (slot < animations.size()) {

View File

@ -385,7 +385,7 @@ void GameData::getNameAndLod(std::string& name, int& lod) {
}
}
Clump* GameData::loadClump(const std::string& name) {
ClumpPtr GameData::loadClump(const std::string& name) {
auto file = index.openFile(name);
if (!file) {
logger->error("Data", "Failed to load model " + name);

View File

@ -133,7 +133,7 @@ public:
/**
* Loads an archived model and returns it directly
*/
Clump* loadClump(const std::string& name);
ClumpPtr loadClump(const std::string& name);
/**
* Loads a DFF and associates its atomics with models.

View File

@ -37,7 +37,7 @@ CharacterObject::CharacterObject(GameWorld* engine, const glm::vec3& pos,
setClump(ClumpPtr(info->getModel()->clone()));
if (info->getModel()) {
setModel(info->getModel());
animator = new Animator(getClump().get());
animator = new Animator(getClump());
createActor();
}
@ -279,7 +279,7 @@ void CharacterObject::changeCharacterModel(const std::string& name) {
setModel(newmodel);
animator = new Animator(getClump().get());
animator = new Animator(getClump());
}
void CharacterObject::updateCharacter(float dt) {

View File

@ -1,8 +1,8 @@
#include <engine/Animator.hpp>
#include <objects/CutsceneObject.hpp>
#include <engine/Animator.hpp>
CutsceneObject::CutsceneObject(GameWorld *engine, const glm::vec3 &pos,
const glm::quat &rot, Clump *model,
const glm::quat &rot, ClumpPtr model,
BaseModelInfo *modelinfo)
: GameObject(engine, pos, rot, modelinfo)
, _parent(nullptr)
@ -14,7 +14,7 @@ CutsceneObject::CutsceneObject(GameWorld *engine, const glm::vec3 &pos,
setModel(getModelInfo<ClumpModelInfo>()->getModel());
}
setClump(ClumpPtr(getModel()->clone()));
animator = new Animator(getClump().get());
animator = new Animator(getClump());
}
CutsceneObject::~CutsceneObject() {

View File

@ -12,7 +12,7 @@ class CutsceneObject : public GameObject, public ClumpObject {
public:
CutsceneObject(GameWorld* engine, const glm::vec3& pos,
const glm::quat& rot, Clump* model,
const glm::quat& rot, ClumpPtr model,
BaseModelInfo* modelinfo);
~CutsceneObject();

View File

@ -31,7 +31,7 @@ class GameObject {
/**
* Model used for rendering
*/
Clump* model_;
ClumpPtr model_;
protected:
void changeModelInfo(BaseModelInfo* next) {
@ -102,14 +102,14 @@ public:
/**
* @return The model used in rendering
*/
Clump* getModel() const {
ClumpPtr getModel() const {
return model_;
}
/**
* Changes the current model, used for re-dressing chars
*/
void setModel(Clump* model) {
void setModel(ClumpPtr model) {
model_ = model;
}

View File

@ -317,7 +317,7 @@ void GameRenderer::renderWorld(GameWorld* world, const ViewCamera& camera,
m, glm::vec3(i.radius +
0.15f * glm::sin(_renderWorld->getGameTime() * 5.f)));
objectRenderer.renderClump(sphereModel, m, nullptr, renderList);
objectRenderer.renderClump(sphereModel.get(), m, nullptr, renderList);
}
// Render arrows above anything that isn't radar only (or hidden)
@ -344,7 +344,7 @@ void GameRenderer::renderWorld(GameWorld* world, const ViewCamera& camera,
glm::vec3(0.f, 0.f, 2.5f + glm::sin(a) * 0.5f));
model = glm::rotate(model, a, glm::vec3(0.f, 0.f, 1.f));
model = glm::scale(model, glm::vec3(1.5f, 1.5f, 1.5f));
objectRenderer.renderClump(arrowModel, model, nullptr, renderList);
objectRenderer.renderClump(arrowModel.get(), model, nullptr, renderList);
}
RW_PROFILE_END();

View File

@ -62,7 +62,7 @@ class GameRenderer {
ViewCamera _camera;
ViewCamera cullingCamera;
bool cullOverride;
/** Number of culling events */
size_t culled;
@ -99,7 +99,7 @@ public:
GLuint getMissingTexture() const {
return m_missingTexture;
}
size_t getCulledCount() {
return culled;
}
@ -174,16 +174,15 @@ public:
*
* GameRenderer will take ownership of the Model* pointer
*/
void setSpecialModel(SpecialModel usage, Clump* model) {
specialmodels_[usage].reset(model);
void setSpecialModel(SpecialModel usage, ClumpPtr model) {
specialmodels_[usage] = model;
}
private:
/// Hard-coded models to use for each of the special models
std::unique_ptr<Clump>
specialmodels_[SpecialModel::SpecialModelCount];
Clump* getSpecialModel(SpecialModel usage) const {
return specialmodels_[usage].get();
ClumpPtr specialmodels_[SpecialModel::SpecialModelCount];
ClumpPtr getSpecialModel(SpecialModel usage) const {
return specialmodels_[usage];
}
};

View File

@ -17,6 +17,7 @@ SET(RWLIB_SOURCES
"source/gl/TextureData.cpp"
"source/rw/abort.cpp"
"source/rw/forward.hpp"
"source/rw/types.hpp"
"source/rw/defines.hpp"

View File

@ -12,18 +12,7 @@
#include <gl/TextureData.hpp>
#include <loaders/RWBinaryStream.hpp>
// Forward Declerations
class ModelFrame;
struct Geometry;
class Atomic;
class Clump;
// Pointer types
using ModelFramePtr = std::shared_ptr<ModelFrame>;
using GeometryPtr = std::shared_ptr<Geometry>;
using AtomicPtr = std::shared_ptr<Atomic>;
using AtomicList = std::vector<AtomicPtr>;
using ClumpPtr = std::shared_ptr<Clump>;
#include <rw/forward.hpp>
/**
* ModelFrame stores transformation hierarchy

View File

@ -439,8 +439,8 @@ AtomicPtr LoaderDFF::readAtomic(FrameList &framelist,
return atomic;
}
Clump *LoaderDFF::loadFromMemory(FileHandle file) {
auto model = new Clump;
ClumpPtr LoaderDFF::loadFromMemory(FileHandle file) {
auto model = std::make_shared<Clump>();
RWBStream rootStream(file->data, file->length);

View File

@ -29,7 +29,7 @@ public:
using GeometryList = std::vector<GeometryPtr>;
using FrameList = std::vector<ModelFramePtr>;
Clump* loadFromMemory(FileHandle file);
ClumpPtr loadFromMemory(FileHandle file);
void setTextureLookupCallback(TextureLookupCallback tlc) {
texturelookup = tlc;

View File

@ -0,0 +1,22 @@
#ifndef RWLIB_FORWARD_HPP
#define RWLIB_FORWARD_HPP
#include <memory>
#include <vector>
// Forward Declarations
class Clump;
class ModelFrame;
struct Geometry;
class Atomic;
class Clump;
// Pointer types
using ModelFramePtr = std::shared_ptr<ModelFrame>;
using GeometryPtr = std::shared_ptr<Geometry>;
using AtomicPtr = std::shared_ptr<Atomic>;
using AtomicList = std::vector<AtomicPtr>;
using ClumpPtr = std::shared_ptr<Clump>;
#endif /* FORWARD_HPP */

View File

@ -103,7 +103,7 @@ void ViewerWidget::paintGL() {
vc.frustum.fov = viewFov;
vc.frustum.aspectRatio = width() / (height() * 1.f);
Clump* model = activeModel;
ClumpPtr model = activeModel;
if (model != _lastModel) {
_lastModel = model;
emit modelChanged(_lastModel);
@ -133,7 +133,7 @@ void ViewerWidget::paintGL() {
ObjectRenderer renderer(world(), vc, 1.f, 0);
RenderList renders;
renderer.renderClump(model, glm::mat4(), nullptr, renders);
renderer.renderClump(model.get(), glm::mat4(), nullptr, renders);
r.getRenderer()->drawBatched(renders);
drawFrameWidget(model->getFrame().get());
@ -165,7 +165,7 @@ void ViewerWidget::drawFrameWidget(ModelFrame* f, const glm::mat4& m) {
glLineWidth(1.f);
}
dp.textures = {whiteTex};
RW_CHECK(renderer != nullptr, "GameRenderer is null");
if(renderer != nullptr) {
renderer->getRenderer()->drawArrays(thisM, _frameWidgetDraw, dp);
@ -207,7 +207,7 @@ void ViewerWidget::showObject(qint16 item) {
}
}
void ViewerWidget::showModel(Clump* model) {
void ViewerWidget::showModel(ClumpPtr model) {
if (dummyObject) gworld->destroyObject(dummyObject);
dummyObject = nullptr;
activeModel = model;
@ -269,7 +269,7 @@ void ViewerWidget::keyReleaseEvent(QKeyEvent* e) {
if (e->key() == Qt::Key_Shift) moveFast = false;
}
Clump* ViewerWidget::currentModel() const {
ClumpPtr ViewerWidget::currentModel() const {
return activeModel;
}

View File

@ -27,12 +27,12 @@ class ViewerWidget : public QGLWidget {
QTimer timer;
GameWorld* gworld;
Clump* activeModel;
ClumpPtr activeModel;
ModelFrame* selectedFrame;
GameObject* dummyObject;
quint16 currentObjectID;
Clump* _lastModel;
ClumpPtr _lastModel;
Animation* canimation;
float viewDistance;
@ -60,7 +60,7 @@ public:
virtual void paintGL();
Clump* currentModel() const;
ClumpPtr currentModel() const;
GameObject* currentObject() const;
GameWorld* world();
@ -68,7 +68,7 @@ public:
public slots:
void showObject(qint16 item);
void showModel(Clump* model);
void showModel(ClumpPtr model);
void selectFrame(ModelFrame* frame);
void exportModel();
@ -81,7 +81,7 @@ signals:
void fileOpened(const QString& file);
void modelChanged(Clump* model);
void modelChanged(ClumpPtr model);
protected:
void keyPressEvent(QKeyEvent*) override;

View File

@ -1,7 +1,7 @@
#include "DFFFramesTreeModel.hpp"
#include <data/Clump.hpp>
DFFFramesTreeModel::DFFFramesTreeModel(Clump* m,
DFFFramesTreeModel::DFFFramesTreeModel(ClumpPtr m,
QObject* parent)
: QAbstractItemModel(parent), model(m) {
}

View File

@ -2,15 +2,14 @@
#ifndef _DFFFRAMESTREEMODEL_HPP_
#define _DFFFRAMESTREEMODEL_HPP_
#include <QAbstractItemModel>
#include <rw/forward.hpp>
#include <rw/types.hpp>
class Clump;
class DFFFramesTreeModel : public QAbstractItemModel {
Clump* model;
ClumpPtr model;
public:
explicit DFFFramesTreeModel(Clump* m, QObject* parent = 0);
explicit DFFFramesTreeModel(ClumpPtr m, QObject* parent = 0);
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;

View File

@ -39,7 +39,7 @@ void ModelViewer::setViewerWidget(ViewerWidget* widget) {
showModel(viewing);
}
void ModelViewer::showModel(Clump* model) {
void ModelViewer::showModel(ClumpPtr model) {
viewing = model;
viewerWidget->showModel(model);
frames->setModel(model);

View File

@ -21,7 +21,7 @@ class Animation;
class ModelViewer : public ViewerInterface {
Q_OBJECT
Clump* viewing;
ClumpPtr viewing;
QSplitter* mainSplit;
QVBoxLayout* mainLayout;
@ -43,7 +43,7 @@ public slots:
/**
* Display a raw model
*/
void showModel(Clump* model);
void showModel(ClumpPtr model);
/**
* Display a game object's model

View File

@ -2,7 +2,7 @@
#include <data/Clump.hpp>
#include <glm/gtx/string_cast.hpp>
void ModelFramesWidget::updateInfoBox(Clump* model, ModelFrame* f) {
void ModelFramesWidget::updateInfoBox(ClumpPtr model, ModelFrame* f) {
if (f == nullptr) {
_frameLabel->setText("");
} else {
@ -33,14 +33,14 @@ ModelFramesWidget::ModelFramesWidget(QWidget* parent, Qt::WindowFlags flags)
setLayout(_layout);
}
void ModelFramesWidget::setModel(Clump* model) {
void ModelFramesWidget::setModel(ClumpPtr model) {
if (framemodel) {
delete framemodel;
framemodel = nullptr;
tree->setModel(nullptr);
}
gmodel = model;
if (model != nullptr) {
if (model.get() != nullptr) {
framemodel = new DFFFramesTreeModel(model, this);
tree->setModel(framemodel);
tree->setDisabled(false);

View File

@ -13,7 +13,7 @@ class ModelFrame;
class ModelFramesWidget : public QWidget {
Q_OBJECT
Clump* gmodel;
ClumpPtr gmodel;
DFFFramesTreeModel* framemodel;
QTreeView* tree;
QVBoxLayout* _layout;
@ -21,7 +21,7 @@ class ModelFramesWidget : public QWidget {
private slots:
void updateInfoBox(Clump* model, ModelFrame* f);
void updateInfoBox(ClumpPtr model, ModelFrame* f);
void selectedModelChanged(const QModelIndex&, const QModelIndex&);
@ -30,7 +30,7 @@ public:
public slots:
void setModel(Clump* model);
void setModel(ClumpPtr model);
signals:

View File

@ -11,9 +11,9 @@ BOOST_AUTO_TEST_CASE(test_load_dff) {
LoaderDFF loader;
Clump* m = loader.loadFromMemory(d);
auto m = loader.loadFromMemory(d);
BOOST_REQUIRE(m != nullptr);
BOOST_REQUIRE(m.get() != nullptr);
BOOST_REQUIRE(m->getFrame());
@ -23,7 +23,7 @@ BOOST_AUTO_TEST_CASE(test_load_dff) {
BOOST_REQUIRE(atomic->getGeometry());
BOOST_REQUIRE(atomic->getFrame());
delete m;
m.reset();
}
}