Explorar o código

Making sure to close files before leaving.

[scan-build]Resource leak.
Cyril Roelandt %!s(int64=14) %!d(string=hai) anos
pai
achega
6a495fc170
Modificáronse 1 ficheiros con 14 adicións e 6 borrados
  1. 14 6
      examples/ppm_downscaler/yuv_downscaler.c

+ 14 - 6
examples/ppm_downscaler/yuv_downscaler.c

@@ -119,20 +119,22 @@ int main(int argc, char **argv)
 /*	fprintf(stderr, "filesize %lx (FRAME SIZE %lx NEW SIZE %lx); nframes %d\n", filesize, FRAMESIZE, NEW_FRAMESIZE, nframes); */
 	assert((filesize % sizeof(struct yuv_frame)) == 0);
 
+	struct yuv_frame *yuv_in_buffer = (struct yuv_frame *) malloc(nframes*FRAMESIZE);
+	assert(yuv_in_buffer);
+
+/*	fprintf(stderr, "Alloc output file ...\n"); */
+	struct yuv_new_frame *yuv_out_buffer = (struct yuv_new_frame *) calloc(nframes, NEW_FRAMESIZE);
+	assert(yuv_out_buffer);
+
 	/* fetch input data */
 	FILE *f_in = fopen(filename_in, "r");
 	assert(f_in);
 
-	struct yuv_frame *yuv_in_buffer = (struct yuv_frame *) malloc(nframes*FRAMESIZE);
-	fread(yuv_in_buffer, FRAMESIZE, nframes, f_in);
-
 	/* allocate room for an output buffer */
 	FILE *f_out = fopen(filename_out, "w+");
 	assert(f_out);
 
-/*	fprintf(stderr, "Alloc output file ...\n"); */
-	struct yuv_new_frame *yuv_out_buffer = (struct yuv_new_frame *) calloc(nframes, NEW_FRAMESIZE);
-	assert(yuv_out_buffer);
+	fread(yuv_in_buffer, FRAMESIZE, nframes, f_in);
 
 	starpu_data_handle *frame_y_handle = (starpu_data_handle *)  calloc(nframes, sizeof(starpu_data_handle));
 	starpu_data_handle *frame_u_handle = (starpu_data_handle *)  calloc(nframes, sizeof(starpu_data_handle));
@@ -279,5 +281,11 @@ int main(int argc, char **argv)
 	/* partition the layers into smaller parts */
 	starpu_shutdown();
 
+	if (fclose(f_in) != 0)
+		fprintf(stderr, "Could not close %s properly\n", filename_in);
+
+	if (fclose(f_out) != 0)
+		fprintf(stderr, "Could not close %s properly\n", filename_out);
+
 	return 0;
 }