From 45cf68a160f94d1091c3e5c69237505b138daf25 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 25 Jun 2008 16:20:40 -0700 Subject: added close method. Fixed all known memory leaks --- src/bmp.c | 14 +++++++++++--- src/gif.c | 11 ++++++++--- src/jpeg.c | 10 ++++++++-- src/main.c | 26 +++++++------------------- src/meh.h | 4 ++-- src/png.c | 10 ++++++++-- src/xlib.c | 3 +-- 7 files changed, 45 insertions(+), 33 deletions(-) diff --git a/src/bmp.c b/src/bmp.c index e071c15..23780f3 100644 --- a/src/bmp.c +++ b/src/bmp.c @@ -10,6 +10,7 @@ struct rgb_t{ struct bmp_t{ struct image img; + FILE *f; unsigned long bitmapoffset; int compression; int bpp; @@ -43,7 +44,7 @@ struct image *bmp_open(FILE *f){ return NULL; b = malloc(sizeof(struct bmp_t)); - b->img.f = f; + b->f = f; fseek(f, 10, SEEK_SET); b->bitmapoffset = getlong(f); @@ -151,7 +152,7 @@ int bmp_read(struct image *img){ unsigned int i, y; unsigned int dy; unsigned char *row; - FILE *f = img->f; + FILE *f = b->f; row = malloc(b->rowwidth); dy = img->width * 3; @@ -170,8 +171,15 @@ int bmp_read(struct image *img){ return 0; } +void bmp_close(struct image *img){ + struct bmp_t *b = (struct bmp_t *)img; + free(b->colours); + fclose(b->f); +} + struct imageformat bmp = { bmp_open, - bmp_read + bmp_read, + bmp_close }; diff --git a/src/gif.c b/src/gif.c index 977e8df..ae15418 100644 --- a/src/gif.c +++ b/src/gif.c @@ -77,13 +77,18 @@ static int gif_read(struct image *img){ img->buf[j++] = colormap[idx].Blue; } - DGifCloseFile(g->gif); - return 0; } +void gif_close(struct image *img){ + struct gif_t *g = (struct gif_t *)img; + DGifCloseFile(g->gif); + fclose(g->f); +} + struct imageformat giflib = { gif_open, - gif_read + gif_read, + gif_close }; diff --git a/src/jpeg.c b/src/jpeg.c index ecefda9..797bdb4 100644 --- a/src/jpeg.c +++ b/src/jpeg.c @@ -92,13 +92,19 @@ static int jpeg_read(struct image *img){ } } jpeg_finish_decompress(&j->cinfo); - jpeg_destroy_decompress(&j->cinfo); return 0; } +void jpeg_close(struct image *img){ + struct jpeg_t *j = (struct jpeg_t *)img; + jpeg_destroy_decompress(&j->cinfo); + fclose(j->f); +} + struct imageformat libjpeg = { jpeg_open, - jpeg_read + jpeg_read, + jpeg_close }; diff --git a/src/main.c b/src/main.c index 016aff1..ecefda4 100644 --- a/src/main.c +++ b/src/main.c @@ -69,7 +69,6 @@ void run(){ struct image *img = NULL; XImage *ximg = NULL; int redraw = 0; - FILE *f = NULL; for(;;){ XEvent event; @@ -78,8 +77,6 @@ void run(){ break; XNextEvent(display, &event); switch(event.type){ - case MapNotify: - break; case ConfigureNotify: if(width != event.xconfigure.width || height != event.xconfigure.height){ width = event.xconfigure.width; @@ -138,18 +135,14 @@ void run(){ if(!img){ const char *firstimg = filename; while(!img){ - if(f){ - fclose(f); - f = NULL; - } - if(!(f = fopen(filename, "rb"))) - fprintf(stderr, "Cannot open '%s'\n", filename); - - if(f){ + FILE *f; + if((f = fopen(filename, "rb"))){ if((img = imgopen(f))) break; else fprintf(stderr, "Invalid format '%s'\n", filename); + }else{ + fprintf(stderr, "Cannot open '%s'\n", filename); } filename = direction(); @@ -158,19 +151,14 @@ void run(){ exit(EXIT_FAILURE); } } - img->buf = NULL; + setaspect(img->width, img->height); - continue; /* make sure setaspect can do its thing */ - } - if(!img->buf){ + img->buf = malloc(3 * img->width * img->height); if(img->fmt->read(img)){ fprintf(stderr, "read error!\n"); } - if(f){ - fclose(f); - f = NULL; - } + img->fmt->close(img); continue; /* Allow for some events to be read, read is slow */ } ximg = getimage(img, width, height); diff --git a/src/meh.h b/src/meh.h index 4190bb4..521e4a9 100644 --- a/src/meh.h +++ b/src/meh.h @@ -6,12 +6,12 @@ struct image; struct imageformat{ struct image *(*open)(FILE *); int (*read)(struct image *); + void (*close)(struct image *); }; struct image{ unsigned char *buf; unsigned int width, height; - FILE *f; struct imageformat *fmt; }; @@ -21,5 +21,5 @@ XImage *ximage(struct image *img, unsigned int width, unsigned int height); void setaspect(unsigned int w, unsigned int h); void xinit(); void drawimage(XImage *ximg, unsigned int width, unsigned int height); -XImage *getimage(struct image *img, int width, int height); +XImage *getimage(struct image *img, unsigned int width, unsigned int height); diff --git a/src/png.c b/src/png.c index 659e935..fa6e9f7 100644 --- a/src/png.c +++ b/src/png.c @@ -96,14 +96,20 @@ int png_read(struct image *img){ row_pointers[y] = img->buf + y * img->width * 3; png_read_image(p->png_ptr, row_pointers); + free(row_pointers); + return 0; +} +void png_close(struct image *img){ + struct png_t *p = (struct png_t *)img; png_destroy_read_struct(&p->png_ptr, &p->info_ptr, &p->end_info); - return 0; + fclose(p->f); } struct imageformat libpng = { png_open, - png_read + png_read, + png_close }; diff --git a/src/xlib.c b/src/xlib.c index 5023cbd..d36a931 100644 --- a/src/xlib.c +++ b/src/xlib.c @@ -85,8 +85,7 @@ XImage *ximage(struct image *img, unsigned int width, unsigned int height) { return ximg; } - -XImage *getimage(struct image *img, int width, int height){ +XImage *getimage(struct image *img, unsigned int width, unsigned int height){ if(width * img->height > height * img->width){ return ximage(img, img->width * height / img->height, height); }else{ -- cgit v1.2.3