diff --git a/compiler2/record_of.c b/compiler2/record_of.c
index 8c6753723e046c23cd4ab958de8531edef79cef8..d762b3eee7587fe5e5f3bf623fadec181fd25ee9 100644
--- a/compiler2/record_of.c
+++ b/compiler2/record_of.c
@@ -976,10 +976,19 @@ void defRecordOfClass1(const struct_of_def *sdef, output_struct *output)
     "  if(p_td.raw->fieldlength || sel_field!=-1){\n"
     "    int a=0;\n"
     "    if(sel_field==-1) sel_field=p_td.raw->fieldlength;\n"
+    "    start_of_field=p_buf.get_pos_bit();\n"
     "    for(a=0;a<sel_field;a++){\n"
     "      decoded_field_length=(*this)[a+start_field].RAW_decode(*p_td.oftype_descr,"
     "p_buf,limit,top_bit_ord,TRUE);\n"
-    "      if(decoded_field_length < 0) return decoded_field_length;\n"
+    "      if(decoded_field_length < 0){\n"
+    "        while(a>=0){\n"
+    "          delete &(*this)[a+start_field];\n"
+    "          a--;\n"
+    "          val_ptr->n_elements--;\n"
+    "        }\n"
+    "        p_buf.set_pos_bit(start_of_field);\n"
+    "        return decoded_field_length;\n"
+    "      }\n"
     "      decoded_length+=decoded_field_length;\n"
     "      limit-=decoded_field_length;\n"
     "    }\n"
@@ -2517,10 +2526,19 @@ void defRecordOfClassMemAllocOptimized(const struct_of_def *sdef, output_struct
     "  if(p_td.raw->fieldlength || sel_field!=-1){\n"
     "    int a=0;\n"
     "    if(sel_field==-1) sel_field=p_td.raw->fieldlength;\n"
+    "    start_of_field=p_buf.get_pos_bit();\n"
     "    for(a=0;a<sel_field;a++){\n"
     "      decoded_field_length=(*this)[a+start_field].RAW_decode(*p_td.oftype_descr,"
     "p_buf,limit,top_bit_ord,TRUE);\n"
-    "      if(decoded_field_length < 0) return decoded_field_length;\n"
+    "      if(decoded_field_length < 0){\n"
+    "        while(a>=0){\n"
+    /*"          delete &(*this)[a+start_field];\n"*/
+    "          a--;\n"
+    "          n_elements--;\n"
+    "        }\n"
+    "        p_buf.set_pos_bit(start_of_field);\n"
+    "        return decoded_field_length;\n"
+    "      }\n"
     "      decoded_length+=decoded_field_length;\n"
     "      limit-=decoded_field_length;\n"
     "    }\n"