diff --git a/gtests/net/packetdrill/code.c b/gtests/net/packetdrill/code.c
index 085683a0dfa6fb734c9d912ef18afa74ab1c5c9e..e0b89e917813f489a6788162f0a8b864ec9ade9f 100644
--- a/gtests/net/packetdrill/code.c
+++ b/gtests/net/packetdrill/code.c
@@ -573,7 +573,7 @@ void run_code_event(struct state *state, struct event *event,
 {
 	DEBUGP("%d: run code event\n", event->line_number);
 
-	char *error = NULL;
+	char *error = NULL, *script_path = NULL;
 
 	/* Wait for the right time before firing off this event. */
 	wait_for_event(state);
@@ -618,7 +618,10 @@ void run_code_event(struct state *state, struct event *event,
 	return;
 
 error_out:
+	script_path = strdup(state->config->script_path);
+	state_free(state);
 	die("%s:%d: runtime error in code: %s\n",
-	    state->config->script_path, event->line_number, error);
+	    script_path, event->line_number, error);
+	free(script_path);
 	free(error);
 }
diff --git a/gtests/net/packetdrill/run.c b/gtests/net/packetdrill/run.c
index bb962e13c18be4018005e442924a6a39bcd3089b..8a40af7bd3e06bcede947cd50815b5f7ef5ef3d5 100644
--- a/gtests/net/packetdrill/run.c
+++ b/gtests/net/packetdrill/run.c
@@ -401,6 +401,7 @@ static void run_local_packet_event(struct state *state, struct event *event,
 		fprintf(stderr, "%s", error);
 		free(error);
 	} else if (result == STATUS_ERR) {
+		state_free(state);
 		die("%s", error);
 	}
 }
@@ -540,8 +541,10 @@ void run_script(struct config *config, struct script *script)
 		wire_client_send_client_starting(state->wire_client);
 
 	while (1) {
-		if (get_next_event(state, &error))
+		if (get_next_event(state, &error)) {
+			state_free(state);
 			die("%s", error);
+		}
 		event = state->event;
 		if (event == NULL)
 			break;
@@ -588,8 +591,11 @@ void run_script(struct config *config, struct script *script)
 		wire_client_next_event(state->wire_client, NULL);
 
 	if (code_execute(state->code, &error)) {
+		char *script_path = strdup(state->config->script_path);
+		state_free(state);
 		die("%s: error executing code: %s\n",
-		    state->config->script_path, error);
+		    script_path, error);
+		free(script_path);
 		free(error);
 	}
 
diff --git a/gtests/net/packetdrill/run_command.c b/gtests/net/packetdrill/run_command.c
index a55e596d218d08ad0063db7bf5ea6d9b2c19c53e..7fbaa9730f752d3056d1f44d27dc43fc39b8679f 100644
--- a/gtests/net/packetdrill/run_command.c
+++ b/gtests/net/packetdrill/run_command.c
@@ -36,6 +36,7 @@
 void run_command_event(
 	struct state *state, struct event *event, struct command_spec *command)
 {
+	char *script_path = NULL;
 	DEBUGP("%d: command: `%s`\n", event->line_number,
 	       command->command_line);
 
@@ -48,8 +49,11 @@ void run_command_event(
 	return;
 
 error_out:
+	script_path = strdup(state->config->script_path);
+	state_free(state);
 	die("%s:%d: error executing `%s` command: %s\n",
-	    state->config->script_path, event->line_number,
+	    script_path, event->line_number,
 	    command->command_line, error);
+	free(script_path);
 	free(error);
 }
diff --git a/gtests/net/packetdrill/run_system_call.c b/gtests/net/packetdrill/run_system_call.c
index 1a606a41893573264ad912a826ad96f47f5249c9..85cb8a15782462f931fad9aaf1ba00cfc23fdd2e 100644
--- a/gtests/net/packetdrill/run_system_call.c
+++ b/gtests/net/packetdrill/run_system_call.c
@@ -1924,7 +1924,7 @@ static void invoke_system_call(
 {
 	DEBUGP("%d: invoke call: %s\n", event->line_number, syscall->name);
 
-	char *error = NULL;
+	char *error = NULL, *script_path = NULL;
 	const char *name = syscall->name;
 	struct expression_list *args = NULL;
 	int i = 0;
@@ -1958,9 +1958,12 @@ static void invoke_system_call(
 	return;
 
 error_out:
+	script_path = strdup(state->config->script_path);
+	state_free(state);
 	die("%s:%d: runtime error in %s call: %s\n",
-	    state->config->script_path, event->line_number,
+	    script_path, event->line_number,
 	    syscall->name, error);
+	free(script_path);
 	free(error);
 }
 
@@ -2007,7 +2010,7 @@ static int yield(void)
 static void enqueue_system_call(
 	struct state *state, struct event *event, struct syscall_spec *syscall)
 {
-	char *error = NULL;
+	char *error = NULL, *script_path = NULL;
 	bool done = false;
 
 	/* Wait if there are back-to-back blocking system calls. */
@@ -2058,9 +2061,12 @@ static void enqueue_system_call(
 	return;
 
 error_out:
+	script_path = strdup(state->config->script_path);
+	state_free(state);
 	die("%s:%d: runtime error in %s call: %s\n",
-	    state->config->script_path, event->line_number,
+	    script_path, event->line_number,
 	    syscall->name, error);
+	free(script_path);
 	free(error);
 }