nsxiv1f01c67
fix: memory leak in img_load_webp (#135)
if `multi.cap` is >0 that means `multi.frames` has already been malloc-ed. by unconditionally malloc-ing again, we're losing all the old memory. this makes it so we're only malloc-ing (or realloc-ing) when needed.
+10 −4
1 file changed
- M image.c +10 −4
| @@ -380,12 +380,18 @@ bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *img) | |||
| 380 | 380 | flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS); | |
| 381 | 381 | img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH); | |
| 382 | 382 | img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT); | |
| 383 | − | img->multi.cap = info.frame_count; | |
| 384 | − | img->multi.sel = 0; | |
| 385 | − | img->multi.frames = emalloc(info.frame_count * sizeof(img_frame_t)); | |
| 383 | + | ||
| 384 | + | if (img->multi.cap == 0) { | |
| 385 | + | img->multi.cap = info.frame_count; | |
| 386 | + | img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t)); | |
| 387 | + | } else if (info.frame_count > img->multi.cap) { | |
| 388 | + | img->multi.cap = info.frame_count; | |
| 389 | + | img->multi.frames = erealloc(img->multi.frames, | |
| 390 | + | img->multi.cap * sizeof(img_frame_t)); | |
| 391 | + | } | |
| 386 | 392 | ||
| 387 | 393 | /* Load and decode frames (also works on images with only 1 frame) */ | |
| 388 | − | img->multi.cnt = 0; | |
| 394 | + | img->multi.cnt = img->multi.sel = 0; | |
| 389 | 395 | while (WebPAnimDecoderGetNext(dec, &buf, &ts)) { | |
| 390 | 396 | im = imlib_create_image_using_copied_data( | |
| 391 | 397 | info.canvas_width, info.canvas_height, (DATA32*)buf); | |