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;
SharedTexture2D loadingScreen;
SharedTexture2D loadingContinueScreen;
std::vector<Light*>* closestLights;
std::vector<shared_ptr<Light>>* closestLights;
std::vector<Flame*> closestFlames;
SharedShaderProgram loadingShader;
SharedShaderProgram lightingShader;

View File

@ -18,9 +18,6 @@ Level::~Level() {
lua_close(luaState);
}
delete(waterPlane);
for(unsigned int i = 0; i<lights.size(); i++) {
delete(lights.at(i));
}
}
void Level::load() {
@ -216,7 +213,7 @@ glm::vec3 Level::getAmbientLight() {
return ambientLight;
}
std::vector<Light*>* Level::getLights() {
std::vector<shared_ptr<Light>>* Level::getLights() {
return &lights;
}
@ -353,7 +350,7 @@ void Level::setCameraCenter(Object* object) {
}
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);
}
@ -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){
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(){
@ -446,7 +443,7 @@ Object* Level::getWaterPlane() {
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()) <
glm::distance(cameraCenter->getPosition(), b->getPosition())) {
return true;
@ -456,13 +453,13 @@ bool Level::compareLightDistances(Light* a, Light* b) {
}
}
std::vector<Light*>* Level::getClosestLights() {
closestLights = std::vector<Light*>(lights);
std::vector<shared_ptr<Light>>* Level::getClosestLights() {
closestLights = std::vector<shared_ptr<Light>>(lights);
std::sort(closestLights.begin(),
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) {
closestLights = std::vector<Light*>(&closestLights[0],
closestLights = std::vector<shared_ptr<Light>>(&closestLights[0],
&closestLights[15]);
}
return &closestLights;

View File

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