From: Matthias Braun Date: Fri, 31 Mar 2006 10:31:03 +0000 (+0000) Subject: fix memory leak in background object X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=4ccc2606d1b037bb385604bdeaa40df7a94eff36;p=supertux.git fix memory leak in background object SVN-Revision: 3144 --- diff --git a/data/levels/world2/christoph2.stl b/data/levels/world2/christoph2.stl index 6dc3262ed..e66f8586d 100644 --- a/data/levels/world2/christoph2.stl +++ b/data/levels/world2/christoph2.stl @@ -129,7 +129,7 @@ (node (x 1984) (y 1568) (time 1)) (node (x 1984) (y 1568) (time 3)) (node (x 1984) (y 1152) (time 1)) - (node (x 1984) (y 1152) (time 1)) + (node (x 1984) (y 1152) (time 3)) ) ) (platform @@ -139,7 +139,7 @@ (node (x 1856) (y 736) (time 1)) (node (x 1856) (y 736) (time 3)) (node (x 1856) (y 1152) (time 1)) - (node (x 1856) (y 1152) (time 1)) + (node (x 1856) (y 1152) (time 3)) ) ) (platform @@ -149,7 +149,7 @@ (node (x 896) (y 736) (time 1)) (node (x 896) (y 736) (time 2)) (node (x 896) (y 288) (time 1)) - (node (x 896) (y 288) (time 1)) + (node (x 896) (y 288) (time 2)) ) ) ) diff --git a/src/object/background.cpp b/src/object/background.cpp index 8e4e2abad..e360c3a27 100644 --- a/src/object/background.cpp +++ b/src/object/background.cpp @@ -29,12 +29,12 @@ #include "msg.hpp" Background::Background() - : type(INVALID), layer(LAYER_BACKGROUND0), image_top(0), image(0), image_bottom(0) + : type(INVALID), layer(LAYER_BACKGROUND0) { } Background::Background(const lisp::Lisp& reader) - : type(INVALID), layer(LAYER_BACKGROUND0), image_top(0), image(0), image_bottom(0) + : type(INVALID), layer(LAYER_BACKGROUND0) { // read position, defaults to (0,0) float px = 0; @@ -47,12 +47,15 @@ Background::Background(const lisp::Lisp& reader) speed_y = 1.0; reader.get("layer", layer); - if(reader.get("image", imagefile) - && reader.get("speed", speed)) { + if(reader.get("image", imagefile) && reader.get("speed", speed)) { set_image(imagefile, speed); reader.get("speed-y", speed_y); - if (reader.get("image-top", imagefile_top)) image_top = new Surface(imagefile_top); - if (reader.get("image-bottom", imagefile_bottom)) image_bottom = new Surface(imagefile_bottom); + if (reader.get("image-top", imagefile_top)) { + image_top.reset(new Surface(imagefile_top)); + } + if (reader.get("image-bottom", imagefile_bottom)) { + image_bottom.reset(new Surface(imagefile_bottom)); + } } else { std::vector bkgd_top_color, bkgd_bottom_color; if(reader.get_vector("top_color", bkgd_top_color) && @@ -64,7 +67,6 @@ Background::Background(const lisp::Lisp& reader) Background::~Background() { - delete image; } void @@ -76,9 +78,13 @@ Background::write(lisp::Writer& writer) writer.start_list("background"); if(type == IMAGE) { - if (image_top) writer.write_string("image-top", imagefile_top); + if (image_top.get() != NULL) + writer.write_string("image-top", imagefile_top); + writer.write_string("image", imagefile); - if (image_bottom) writer.write_string("image-bottom", imagefile_bottom); + if (image_bottom.get() != NULL) + writer.write_string("image-bottom", imagefile_bottom); + writer.write_float("speed", speed); writer.write_float("speed-y", speed_y); } else if(type == GRADIENT) { @@ -109,8 +115,7 @@ Background::set_image(const std::string& name, float speed) this->imagefile = name; this->speed = speed; - delete image; - image = new Surface(name); + image.reset(new Surface(name)); } void @@ -127,8 +132,7 @@ Background::set_gradient(Color top, Color bottom) || gradient_bottom.blue > 1.0 || gradient_bottom.alpha > 1.0) msg_warning("bottom gradient color has values above 1.0"); - delete image; - image = NULL; + image.release(); } void @@ -140,7 +144,7 @@ Background::draw(DrawingContext& context) context.draw_gradient(gradient_top, gradient_bottom, layer); context.pop_transform(); } else if(type == IMAGE) { - if(!image) + if(image.get() == NULL) return; int w = (int) image->get_width(); @@ -153,15 +157,15 @@ Background::draw(DrawingContext& context) context.set_translation(Vector(0, 0)); for(int x = sx; x < SCREEN_WIDTH; x += w) { for(int y = sy; y < SCREEN_HEIGHT; y += h) { - if (image_top && (y < center_image_py)) { - context.draw_surface(image_top, Vector(x, y), layer); + if (image_top.get() != NULL && (y < center_image_py)) { + context.draw_surface(image_top.get(), Vector(x, y), layer); continue; } - if (image_bottom && (y >= bottom_image_py)) { - context.draw_surface(image_bottom, Vector(x, y), layer); + if (image_bottom.get() != NULL && (y >= bottom_image_py)) { + context.draw_surface(image_bottom.get(), Vector(x, y), layer); continue; } - context.draw_surface(image, Vector(x, y), layer); + context.draw_surface(image.get(), Vector(x, y), layer); } } context.pop_transform(); diff --git a/src/object/background.hpp b/src/object/background.hpp index 28b14cf3e..ac268a971 100644 --- a/src/object/background.hpp +++ b/src/object/background.hpp @@ -19,6 +19,7 @@ #ifndef SUPERTUX_BACKGROUND_H #define SUPERTUX_BACKGROUND_H +#include #include "video/surface.hpp" #include "video/drawing_context.hpp" #include "game_object.hpp" @@ -69,9 +70,9 @@ private: Vector pos; /**< coordinates of upper-left corner of image */ float speed; /**< scroll-speed in horizontal direction */ float speed_y; /**< scroll-speed in vertical direction */ - Surface* image_top; /**< image to draw above pos */ - Surface* image; /**< image to draw, anchored at pos */ - Surface* image_bottom; /**< image to draw below pos+ */ + std::auto_ptr image_top; /**< image to draw above pos */ + std::auto_ptr image; /**< image to draw, anchored at pos */ + std::auto_ptr image_bottom; /**< image to draw below pos+ */ Color gradient_top, gradient_bottom; };