Program: 12197
Project: 12197
Buggy source:
/** 
 * This deallocates the given instantiation. This should only be invoked via the possibly_deallocate_instantiation() macro. <p>recmem.cpp:757:deallocate_instantiation
 * @param inst
 */
private void deallocate_instantiation(Instantiation inst){
  int level=inst.match_goal_level;
  for (Condition cond=inst.top_of_instantiated_conditions; cond != null; cond=cond.next) {
    final PositiveCondition pc=cond.asPositiveCondition();
    if (pc != null) {
      final BackTraceInfo bt=pc.bt();
      if (bt.hasProhibits()) {
        for (        Preference pref : bt) {
          if (SoarConstants.DO_TOP_LEVEL_REF_CTS) {
            pref.preference_remove_ref(this);
          }
 else {
            if (level > SoarConstants.TOP_GOAL_LEVEL)             pref.preference_remove_ref(this);
          }
        }
        bt.clearProhibits();
      }
      if (SoarConstants.DO_TOP_LEVEL_REF_CTS) {
        if (bt.trace != null) {
          bt.trace.preference_remove_ref(this);
          bt.trace=null;
        }
      }
 else {
        if (level > SoarConstants.TOP_GOAL_LEVEL) {
          if (bt.trace != null) {
            bt.trace.preference_remove_ref(this);
            bt.trace=null;
          }
        }
      }
    }
  }
  inst.top_of_instantiated_conditions=null;
  inst.bottom_of_instantiated_conditions=null;
  inst.nots=null;
}

Fixed version (ground truth):
/** 
 * This deallocates the given instantiation. This should only be invoked via the possibly_deallocate_instantiation() macro. <p>recmem.cpp:757:deallocate_instantiation
 * @param inst
 */
private void deallocate_instantiation(Instantiation inst){
  Condition cond;
  int level;
  Stack<Condition> cond_stack=new Stack<Condition>();
  List<Instantiation> inst_list=new ArrayList<Instantiation>();
  inst_list.add(inst);
  ListIterator<Instantiation> next_iter=inst_list.listIterator();
  while (next_iter.hasNext()) {
    inst=next_iter.next();
    assert(inst != null);
    level=inst.match_goal_level;
    for (cond=inst.top_of_instantiated_conditions; cond != null; cond=cond.next) {
      final PositiveCondition pc=cond.asPositiveCondition();
      if (pc != null) {
        final BackTraceInfo bt=pc.bt();
        if (bt.hasProhibits()) {
          for (          Preference pref : bt) {
            if (SoarConstants.DO_TOP_LEVEL_REF_CTS) {
              pref.preference_remove_ref(this);
            }
 else {
              if (level > SoarConstants.TOP_GOAL_LEVEL)               pref.preference_remove_ref(this);
            }
          }
          bt.clearProhibits();
        }
        if (!SoarConstants.DO_TOP_LEVEL_REF_CTS && level <= SoarConstants.TOP_GOAL_LEVEL) {
          continue;
        }
 else {
          final Preference trace=bt.trace;
          if (trace != null) {
            trace.reference_count--;
            if (trace.reference_count == 0) {
              if (trace.reference_count != 0) {
                continue;
              }
              boolean has_active_clones=false;
              for (Preference clone=trace.next_clone; clone != null; clone=clone.next_clone) {
                if (clone.reference_count != 0) {
                  has_active_clones=true;
                }
              }
              if (has_active_clones) {
                continue;
              }
              for (Preference clone=trace.prev_clone; clone != null; clone=clone.prev_clone) {
                if (clone.reference_count != 0) {
                  has_active_clones=true;
                }
              }
              if (has_active_clones) {
                continue;
              }
{
                Preference clone=trace.next_clone;
                Preference next;
                while (clone != null) {
                  next=clone.next_clone;
                  Preference.deallocate_preference(clone,this);
                  clone=next;
                }
                clone=trace.prev_clone;
                while (clone != null) {
                  next=clone.prev_clone;
                  Preference.deallocate_preference(clone,this);
                  clone=next;
                }
              }
              if (trace.on_goal_list) {
                if (trace.all_of_goal_next != null) {
                  trace.all_of_goal_next.all_of_goal_prev=trace.all_of_goal_prev;
                }
                if (trace.all_of_goal_prev != null) {
                  trace.all_of_goal_prev.all_of_goal_next=trace.all_of_goal_next;
                }
 else {
                  trace.inst.match_goal.goalInfo.preferences_from_goal=trace.all_of_goal_next;
                }
              }
              if (trace.inst_next != null) {
                trace.inst_next.inst_prev=trace.inst_prev;
              }
              if (trace.inst_prev != null) {
                trace.inst_prev.inst_next=trace.inst_next;
              }
 else {
                trace.inst.preferences_generated=trace.inst_next;
              }
              if ((trace.inst.preferences_generated == null) && (!trace.inst.in_ms)) {
                int index=next_iter.previousIndex() + 1;
                inst_list.add(index,trace.inst);
                next_iter=inst_list.listIterator(index);
              }
              cond_stack.push(cond);
            }
          }
        }
      }
    }
  }
  for (  Condition temp : cond_stack) {
    temp.asPositiveCondition().bt().trace=null;
  }
  for (  Instantiation temp : inst_list) {
    temp.top_of_instantiated_conditions=null;
    temp.bottom_of_instantiated_conditions=null;
    temp.nots=null;
  }
}