}
     }
 
+  objfile *objfile = data->module->objfile;
+  gdb_assert (objfile != nullptr);
+
   /* We have to make a copy of the name so that we can unlink the
      underlying file -- removing the objfile will cause the name to be
      freed, so we can't simply keep a reference to it.  */
-  std::string objfile_name_s = objfile_name (data->module->objfile);
-  for (objfile *objfile : current_program_space->objfiles ())
-    if ((objfile->flags & OBJF_USERLOADED) == 0
-       && objfile_name_s == objfile_name (objfile))
-      {
-       objfile->unlink ();
-
-       /* It may be a bit too pervasive in this dummy_frame dtor callback.  */
-       clear_symtab_users (0);
-
-       break;
-      }
+  std::string objfile_name_s = objfile_name (objfile);
+
+  objfile->unlink ();
+
+  /* It may be a bit too pervasive in this dummy_frame dtor callback.  */
+  clear_symtab_users (0);
 
   /* Delete the .c file.  */
   unlink (data->module->source_file.c_str ());