diff --git a/gdb/infrun.c b/gdb/infrun.c
index 9c4a07daba97..1fee65730dbc 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1548,7 +1548,7 @@ get_displaced_step_closure_by_addr (CORE_ADDR addr)
   /* If checking the mode of displaced instruction in copy area.  */
   if (displaced->step_thread != nullptr
       && displaced->step_copy == addr)
-    return displaced->step_closure;
+    return displaced->step_closure.get ();
 
   return NULL;
 }
@@ -1606,13 +1606,9 @@ use_displaced_stepping (struct thread_info *tp)
 
 /* Clean out any stray displaced stepping state.  */
 static void
-displaced_step_clear (struct displaced_step_inferior_state *displaced)
+displaced_step_clear (displaced_step_inferior_state *displaced)
 {
-  /* Indicate that there is no cleanup pending.  */
-  displaced->step_thread = nullptr;
-
-  delete displaced->step_closure;
-  displaced->step_closure = NULL;
+  displaced->reset ();
 }
 
 /* A cleanup that wraps displaced_step_clear.  */
@@ -1762,7 +1758,7 @@ displaced_step_prepare_throw (thread_info *tp)
      succeeds.  */
   displaced->step_thread = tp;
   displaced->step_gdbarch = gdbarch;
-  displaced->step_closure = closure;
+  displaced->step_closure.reset (closure);
   displaced->step_original = original;
   displaced->step_copy = copy;
 
@@ -1887,7 +1883,7 @@ displaced_step_fixup (thread_info *event_thread, enum gdb_signal signal)
     {
       /* Fix up the resulting state.  */
       gdbarch_displaced_step_fixup (displaced->step_gdbarch,
-                                    displaced->step_closure,
+                                    displaced->step_closure.get (),
                                     displaced->step_original,
                                     displaced->step_copy,
                                     get_thread_regcache (displaced->step_thread));
@@ -2480,8 +2476,8 @@ resume_1 (enum gdb_signal sig)
 	  pc = regcache_read_pc (get_thread_regcache (tp));
 
 	  displaced = get_displaced_stepping_state (tp->inf);
-	  step = gdbarch_displaced_step_hw_singlestep (gdbarch,
-						       displaced->step_closure);
+	  step = gdbarch_displaced_step_hw_singlestep
+	    (gdbarch, displaced->step_closure.get ());
 	}
     }
 
@@ -5313,6 +5309,15 @@ Cannot fill $_exitsignal with the correct signal number.\n"));
 	    struct regcache *child_regcache;
 	    CORE_ADDR parent_pc;
 
+	    if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
+	      {
+		struct displaced_step_inferior_state *displaced
+		  = get_displaced_stepping_state (parent_inf);
+
+		/* Restore scratch pad for child process.  */
+		displaced_step_restore (displaced, ecs->ws.value.related_pid);
+	      }
+
 	    /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED,
 	       indicating that the displaced stepping of syscall instruction
 	       has been done.  Perform cleanup for parent process here.  Note
@@ -5323,15 +5328,6 @@ Cannot fill $_exitsignal with the correct signal number.\n"));
 	       that needs it.  */
 	    start_step_over ();
 
-	    if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
-	      {
-		struct displaced_step_inferior_state *displaced
-		  = get_displaced_stepping_state (parent_inf);
-
-		/* Restore scratch pad for child process.  */
-		displaced_step_restore (displaced, ecs->ws.value.related_pid);
-	      }
-
 	    /* Since the vfork/fork syscall instruction was executed in the scratchpad,
 	       the child's PC is also within the scratchpad.  Set the child's PC
 	       to the parent's PC value, which has already been fixed up.
diff --git a/gdb/infrun.h b/gdb/infrun.h
index 8040b28f0172..c6329c844d9b 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -290,7 +290,7 @@ struct displaced_step_inferior_state
     failed_before = 0;
     step_thread = nullptr;
     step_gdbarch = nullptr;
-    step_closure = nullptr;
+    step_closure.reset ();
     step_original = 0;
     step_copy = 0;
     step_saved_copy.clear ();
@@ -310,7 +310,7 @@ struct displaced_step_inferior_state
 
   /* The closure provided gdbarch_displaced_step_copy_insn, to be used
      for post-step cleanup.  */
-  displaced_step_closure *step_closure;
+  std::unique_ptr<displaced_step_closure> step_closure;
 
   /* The address of the original instruction, and the copy we
      made.  */
