diff options
author | Thomas White <taw@bitwiz.org.uk> | 2011-10-14 00:00:32 +0200 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2011-10-14 00:00:32 +0200 |
commit | 4a4fd16aa69ae95afc4715ce371b2f714333fc82 (patch) | |
tree | 14f917782409383655ff9ec2c63d5d35b1cc1775 | |
parent | 4ae63de59d426f06d03c6e1ba678cc472599c936 (diff) |
Fix memory madness
-rw-r--r-- | src/objects.c | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/objects.c b/src/objects.c index e926dcc..5fd234b 100644 --- a/src/objects.c +++ b/src/objects.c @@ -38,18 +38,23 @@ struct image_store { int n_images; - struct image *images; + struct image **images; }; static struct image *add_image_to_store(struct image_store *is, char *filename) { - struct image *images_new; + struct image **images_new; + struct image *i_new; int idx; GError *error = NULL; int w, h; - images_new = realloc(is->images, (is->n_images+1)*sizeof(struct image)); + i_new = calloc(1, sizeof(struct image)); + if ( i_new == NULL ) return NULL; + + images_new = realloc(is->images, + (is->n_images+1)*sizeof(struct image *)); if ( images_new == NULL ) { fprintf(stderr, "Couldn't allocate memory for image.\n"); return NULL; @@ -60,18 +65,20 @@ static struct image *add_image_to_store(struct image_store *is, char *filename) gdk_pixbuf_get_file_info(filename, &w, &h); /* FIXME: If image is huge, load a smaller version */ - is->images[idx].pb = gdk_pixbuf_new_from_file(filename, &error); - if ( is->images[idx].pb == NULL ) { + i_new->pb = gdk_pixbuf_new_from_file(filename, &error); + if ( i_new->pb == NULL ) { fprintf(stderr, "Failed to load image '%s'\n", filename); is->n_images--; return NULL; } - is->images[idx].filename = filename; - is->images[idx].refcount = 1; - is->images[idx].width = w; - is->images[idx].height = h; + i_new->filename = filename; + i_new->refcount = 1; + i_new->width = w; + i_new->height = h; + + is->images[idx] = i_new; - return &is->images[idx]; + return i_new; } @@ -80,8 +87,8 @@ static struct image *find_filename(struct image_store *is, const char *filename) int i; for ( i=0; i<is->n_images; i++ ) { - if ( strcmp(is->images[i].filename, filename) == 0 ) { - return &is->images[i]; + if ( strcmp(is->images[i]->filename, filename) == 0 ) { + return is->images[i]; } } |