diff --git a/src/main.c b/src/main.c
index e266351a29e43b5ee98f45f980888f339a947a5b..8212c028f67f19d7b257fada1f443e35ea6f610f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5064,13 +5064,16 @@ quit_curl:
   if(config->trace_fopened && config->trace_stream)
     fclose(config->trace_stream);
 
+  /* Dump the libcurl code if previously enabled.
+     NOTE: that this function relies on config->errors amongst other things
+     so not everything can be closed and cleaned before this is called */
+  dumpeasycode(config);
+
   if(config->errors_fopened)
     fclose(config->errors);
 
   main_free(); /* cleanup */
 
-  dumpeasycode(config);
-
   return res;
 }