cmd/covdata: close cpu and mem profile

Change-Id: Iaf14989eb2981f724c4091f992ed99687ce3a60e
GitHub-Last-Rev: 6e6c82bb8fba5c32c24eafb50eec6751a93d09f9
GitHub-Pull-Request: golang/go#66852
Reviewed-on: https://go-review.googlesource.com/c/go/+/579255
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Auto-Submit: Than McIntosh <thanm@google.com>
diff --git a/src/cmd/covdata/covdata.go b/src/cmd/covdata/covdata.go
index 95bc30d..549efea 100644
--- a/src/cmd/covdata/covdata.go
+++ b/src/cmd/covdata/covdata.go
@@ -176,7 +176,12 @@
 		if err := pprof.StartCPUProfile(f); err != nil {
 			fatal("%v", err)
 		}
-		atExit(pprof.StopCPUProfile)
+		atExit(func() {
+			pprof.StopCPUProfile()
+			if err = f.Close(); err != nil {
+				fatal("error closing cpu profile: %v", err)
+			}
+		})
 	}
 	if *memprofileflag != "" {
 		if *memprofilerateflag != 0 {
@@ -192,6 +197,9 @@
 			if err := pprof.Lookup("heap").WriteTo(f, writeLegacyFormat); err != nil {
 				fatal("%v", err)
 			}
+			if err = f.Close(); err != nil {
+				fatal("error closing memory profile: %v", err)
+			}
 		})
 	} else {
 		// Not doing memory profiling; disable it entirely.