diff options
author | John Hawthorn <jhawthor@uvic.ca> | 2008-06-25 16:20:40 -0700 |
---|---|---|
committer | John Hawthorn <jhawthor@uvic.ca> | 2008-06-25 16:20:40 -0700 |
commit | 45cf68a160f94d1091c3e5c69237505b138daf25 (patch) | |
tree | 33b3c29257f6f983b90174caeaa03f60c856de69 | |
parent | 665c97078cfa687273e89c8b969d08f5290bcc01 (diff) | |
download | mirror-meh-45cf68a160f94d1091c3e5c69237505b138daf25.tar.gz mirror-meh-45cf68a160f94d1091c3e5c69237505b138daf25.tar.bz2 mirror-meh-45cf68a160f94d1091c3e5c69237505b138daf25.zip |
added close method. Fixed all known memory leaks
-rw-r--r-- | src/bmp.c | 14 | ||||
-rw-r--r-- | src/gif.c | 11 | ||||
-rw-r--r-- | src/jpeg.c | 10 | ||||
-rw-r--r-- | src/main.c | 26 | ||||
-rw-r--r-- | src/meh.h | 4 | ||||
-rw-r--r-- | src/png.c | 10 | ||||
-rw-r--r-- | src/xlib.c | 3 |
7 files changed, 45 insertions, 33 deletions
@@ -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 }; @@ -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 }; @@ -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 }; @@ -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); @@ -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); @@ -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 }; @@ -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{ |