From 8e284a70b7cca7d1e8e74b4cf2a54a2754ea8512 Mon Sep 17 00:00:00 2001 From: ehofman Date: Thu, 27 Jan 2005 10:42:31 +0000 Subject: [PATCH] Melchior FRANZ: Trying to find the bug in tower.cxx (that crashes fgfs quite frequently for me!), I'm playing with valgrind again. Until I'm in the ATC subsystem there will be some other bugs and nitpicking along the way. valgrind doesn't like that imgage->tmp is once allocated with new and once with new[], sometimes with malloc() (via map), and sometimes freed with delete (not delete[]!) and sometimes with free(). With simple types such as GLubyte this shouldn't really make a difference, but anyway. Also, I promised that I'd send patches for "if (foo) delete foo;" as I'm making other changes to concerned files. texture.cxx is one with a few occurrences thereof. (Remember: C++ explicitly allows to delete null-pointers, so this check is redundant, and hence not tolerated in other projects, such as KDE. Doesn't have to impress us, of course. :-) Also, fixes 4 signed/unsigned warnings (gcc 3.3.4) --- simgear/screen/texture.cxx | 75 +++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/simgear/screen/texture.cxx b/simgear/screen/texture.cxx index 2544354e..184f6e2d 100644 --- a/simgear/screen/texture.cxx +++ b/simgear/screen/texture.cxx @@ -49,9 +49,7 @@ SGTexture::SGTexture(unsigned int width, unsigned int height) SGTexture::~SGTexture() { - if ( texture_data ) { - delete texture_data; - } + delete texture_data; if ( texture_id != 0 ) { free_id(); @@ -182,8 +180,7 @@ SGTexture::read_alpha_texture(const char *name) SGTexture::ImageRec *image; int y; - if (texture_data) - delete texture_data; + delete[] texture_data; image = ImageOpen(name); if(!image) { @@ -225,8 +222,7 @@ SGTexture::read_rgb_texture(const char *name) SGTexture::ImageRec *image; int y; - if (texture_data) - delete texture_data; + delete[] texture_data; image = ImageOpen(name); if(!image) { @@ -249,11 +245,11 @@ SGTexture::read_rgb_texture(const char *name) bbuf = new GLubyte[ image->xsize ]; abuf = new GLubyte[ image->xsize ]; if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) { - delete texture_data; - delete rbuf; - delete gbuf; - delete bbuf; - delete abuf; + delete[] texture_data; + delete[] rbuf; + delete[] gbuf; + delete[] bbuf; + delete[] abuf; errstr = OUT_OF_MEMORY; return; } @@ -277,10 +273,10 @@ SGTexture::read_rgb_texture(const char *name) } ImageClose(image); - delete rbuf; - delete gbuf; - delete bbuf; - delete abuf; + delete[] rbuf; + delete[] gbuf; + delete[] bbuf; + delete[] abuf; } @@ -293,8 +289,7 @@ SGTexture::read_rgba_texture(const char *name) SGTexture::ImageRec *image; int y; - if (texture_data) - delete texture_data; + delete[] texture_data; image = ImageOpen(name); if(!image) { @@ -317,11 +312,11 @@ SGTexture::read_rgba_texture(const char *name) bbuf = new GLubyte[ image->xsize ]; abuf = new GLubyte[ image->xsize ]; if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) { - delete texture_data; - delete rbuf; - delete gbuf; - delete bbuf; - delete abuf; + delete[] texture_data; + delete[] rbuf; + delete[] gbuf; + delete[] bbuf; + delete[] abuf; errstr = OUT_OF_MEMORY; return; } @@ -346,10 +341,10 @@ SGTexture::read_rgba_texture(const char *name) } ImageClose(image); - delete rbuf; - delete gbuf; - delete bbuf; - delete abuf; + delete[] rbuf; + delete[] gbuf; + delete[] bbuf; + delete[] abuf; } void @@ -359,8 +354,7 @@ SGTexture::read_raw_texture(const char *name) SGTexture::ImageRec *image; int y; - if (texture_data) - delete texture_data; + delete[] texture_data; image = RawImageOpen(name); @@ -394,8 +388,7 @@ SGTexture::read_r8_texture(const char *name) SGTexture::ImageRec *image; int xy; - if (texture_data) - delete texture_data; + delete[] texture_data; //it wouldn't make sense to write a new function ... image = RawImageOpen(name); @@ -544,7 +537,7 @@ void SGTexture::ImageClose(SGTexture::ImageRec *image) { if (image->file) gzclose(image->file); if (file) fclose(file); - delete image->tmp; + delete[] image->tmp; delete image; } @@ -585,7 +578,7 @@ SGTexture::RawImageOpen(const char *fileName) //just allocate a pseudo value as I'm too lazy to change ImageClose()... - image->tmp = new GLubyte; + image->tmp = new GLubyte[1]; if (image->tmp == 0) { errstr = OUT_OF_MEMORY; @@ -827,7 +820,7 @@ SGTexture::make_grayscale(float contrast) { return; int colors = (num_colors == 3) ? 1 : 2; - GLubyte *map = (GLubyte *)malloc (texture_width * texture_height * colors); + GLubyte *map = new GLubyte[ texture_width * texture_height * colors ]; for (int y=0; y 3); int tsize = texture_width * texture_height * colors; - GLubyte *map = (GLubyte *)malloc (tsize); + GLubyte *map = new GLubyte[ tsize ]; int mpos = 0, dpos = 0; for (int y=0; y