Fixing a memory leak by converting pointers to smart pointers.

This commit is contained in:
Faerbit 2015-05-31 21:17:46 +02:00
parent b8db45fc7c
commit 53e8e6d719
3 changed files with 14 additions and 17 deletions

View File

@ -53,7 +53,7 @@ class Graphics {
std::string loadingScreenContinuePath; std::string loadingScreenContinuePath;
SharedTexture2D loadingScreen; SharedTexture2D loadingScreen;
SharedTexture2D loadingContinueScreen; SharedTexture2D loadingContinueScreen;
std::vector<Light*>* closestLights; std::vector<shared_ptr<Light>>* closestLights;
std::vector<Flame*> closestFlames; std::vector<Flame*> closestFlames;
SharedShaderProgram loadingShader; SharedShaderProgram loadingShader;
SharedShaderProgram lightingShader; SharedShaderProgram lightingShader;

View File

@ -18,9 +18,6 @@ Level::~Level() {
lua_close(luaState); lua_close(luaState);
} }
delete(waterPlane); delete(waterPlane);
for(unsigned int i = 0; i<lights.size(); i++) {
delete(lights.at(i));
}
} }
void Level::load() { void Level::load() {
@ -216,7 +213,7 @@ glm::vec3 Level::getAmbientLight() {
return ambientLight; return ambientLight;
} }
std::vector<Light*>* Level::getLights() { std::vector<shared_ptr<Light>>* Level::getLights() {
return &lights; return &lights;
} }
@ -353,7 +350,7 @@ void Level::setCameraCenter(Object* object) {
} }
void Level::addLight(Light light) { void Level::addLight(Light light) {
Light *add_light = new Light(light); shared_ptr<Light> add_light = shared_ptr<Light>(new Light(light));
this->lights.push_back(add_light); this->lights.push_back(add_light);
} }
@ -363,7 +360,7 @@ void Level::preloadLightPosition(float xPos, float yPos, float zPos){
void Level::addLightByParameters(float redColour, float greenColour, float blueColour, float intensity, float flameYOffset, float flameHeight, float flameWidth){ void Level::addLightByParameters(float redColour, float greenColour, float blueColour, float intensity, float flameYOffset, float flameHeight, float flameWidth){
glm::vec3 colour = glm::vec3(redColour, greenColour, blueColour); glm::vec3 colour = glm::vec3(redColour, greenColour, blueColour);
this->lights.push_back(new Light(nextLightPosition, colour, intensity, flameYOffset, flameHeight, flameWidth)); this->addLight(Light(nextLightPosition, colour, intensity, flameYOffset, flameHeight, flameWidth));
} }
void Level::deleteFourLights(){ void Level::deleteFourLights(){
@ -446,7 +443,7 @@ Object* Level::getWaterPlane() {
return waterPlane; return waterPlane;
} }
bool Level::compareLightDistances(Light* a, Light* b) { bool Level::compareLightDistances(shared_ptr<Light> a, shared_ptr<Light> b) {
if (glm::distance(cameraCenter->getPosition(), a->getPosition()) < if (glm::distance(cameraCenter->getPosition(), a->getPosition()) <
glm::distance(cameraCenter->getPosition(), b->getPosition())) { glm::distance(cameraCenter->getPosition(), b->getPosition())) {
return true; return true;
@ -456,13 +453,13 @@ bool Level::compareLightDistances(Light* a, Light* b) {
} }
} }
std::vector<Light*>* Level::getClosestLights() { std::vector<shared_ptr<Light>>* Level::getClosestLights() {
closestLights = std::vector<Light*>(lights); closestLights = std::vector<shared_ptr<Light>>(lights);
std::sort(closestLights.begin(), std::sort(closestLights.begin(),
closestLights.end(), closestLights.end(),
[this](Light* a, Light* b) {return compareLightDistances(a, b); }); [this](shared_ptr<Light> a, shared_ptr<Light> b) {return compareLightDistances(a, b); });
if (lights.size() > 15) { if (lights.size() > 15) {
closestLights = std::vector<Light*>(&closestLights[0], closestLights = std::vector<shared_ptr<Light>>(&closestLights[0],
&closestLights[15]); &closestLights[15]);
} }
return &closestLights; return &closestLights;

View File

@ -37,7 +37,7 @@ class Level {
std::vector<glm::mat4>* shadowVPs=0); std::vector<glm::mat4>* shadowVPs=0);
glm::vec3 getAmbientLight(); glm::vec3 getAmbientLight();
Light* getDirectionalLight(); Light* getDirectionalLight();
std::vector<Light*>* getLights(); std::vector<shared_ptr<Light>>* getLights();
Object* getCameraCenter(); Object* getCameraCenter();
Camera* getCamera(); Camera* getCamera();
glm::vec3 getCameraPosition(); glm::vec3 getCameraPosition();
@ -84,7 +84,7 @@ class Level {
void addToSpecificChunk(Object* object, int xPosition, int zPosition); void addToSpecificChunk(Object* object, int xPosition, int zPosition);
void enqueueObjects(Graphics* graphics); void enqueueObjects(Graphics* graphics);
void sortObjects(int textureCount); void sortObjects(int textureCount);
std::vector<Light*>* getClosestLights(); std::vector<shared_ptr<Light>>* getClosestLights();
private: private:
std::vector<Chunk*> getSurroundingChunks(int chunkRenderDistance); std::vector<Chunk*> getSurroundingChunks(int chunkRenderDistance);
lua_State* luaState=nullptr; lua_State* luaState=nullptr;
@ -93,8 +93,8 @@ class Level {
std::vector<Object*> allObjects; std::vector<Object*> allObjects;
std::vector<Object*> physicsObjects; std::vector<Object*> physicsObjects;
std::vector<std::vector<Chunk>> chunks; std::vector<std::vector<Chunk>> chunks;
std::vector<Light*> lights; std::vector<shared_ptr<Light>> lights;
std::vector<Light*> closestLights; std::vector<shared_ptr<Light>> closestLights;
std::vector<Trigger> triggers; std::vector<Trigger> triggers;
Object* waterPlane=nullptr; Object* waterPlane=nullptr;
glm::vec3 ambientLight; glm::vec3 ambientLight;
@ -114,7 +114,7 @@ class Level {
glm::vec3 nextLightPosition; glm::vec3 nextLightPosition;
int chunkSize; int chunkSize;
float farPlane; float farPlane;
bool compareLightDistances(Light* a, Light* b); bool compareLightDistances(shared_ptr<Light> a, shared_ptr<Light> b);
}; };
#endif #endif