diff --git a/src/library.cpp b/src/library.cpp
index 0544aa5a4a3538143cc7b7a8ec436b25f66d3964..f0bd7f7ca6c9a5b09a028096b659697233760366 100644
--- a/src/library.cpp
+++ b/src/library.cpp
@@ -276,13 +276,6 @@ void lammps_commands_string(void *ptr, char *str)
 {
   LAMMPS *lmp = (LAMMPS *) ptr;
 
-  BEGIN_CAPTURE
-  {
-    if (lmp->update->whichflag != 0)
-      lmp->error->all(FLERR,"Library error: issuing LAMMPS command during run");
-  }
-  END_CAPTURE
-
   // make copy of str so can strtok() it
 
   int n = strlen(str) + 1;
@@ -291,6 +284,11 @@ void lammps_commands_string(void *ptr, char *str)
 
   BEGIN_CAPTURE
   {
+    if (lmp->update->whichflag != 0) {
+      delete [] copy;
+      lmp->error->all(FLERR,"Library error: issuing LAMMPS command during run");
+    }
+
     char *ptr = copy;
     for (int i=0; i < n-1; ++i) {