Program: 97518
Project: 97518
Buggy source:
private void saveAndCloseManagedViews(){
  Set<String> managedViewIds=determineManagedViews();
  IWorkbench workbench=PlatformUI.getWorkbench();
  if (managedArea.getInstalledBundles().size() > 0 && workbench != null) {
    IWorkbenchPage page=null;
    if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() == null) {
      IWorkbenchWindow[] windows=workbench.getWorkbenchWindows();
      for (      IWorkbenchWindow win : windows) {
        page=win.getActivePage();
        if (page != null) {
          break;
        }
      }
    }
 else {
      page=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    }
    if (page == null) {
      return;
    }
    IPerspectiveDescriptor originalPerspective=page.getPerspective();
    XMLMemento memento=XMLMemento.createWriteRoot(TAG_OTE_PRECOMPILED);
    IPerspectiveDescriptor[] pd=page.getOpenPerspectives();
    for (int i=0; i < pd.length; i++) {
      try {
        page.setPerspective(pd[i]);
      }
 catch (      Exception ex) {
      }
      IMemento perspectiveMemento=null;
      try {
        perspectiveMemento=memento.createChild(TAG_PERSPECTIVE);
        perspectiveMemento.putString("String_Node_Str",pd[i].getId());
      }
 catch (      Exception ex) {
      }
      IViewReference[] activeReferences=page.getViewReferences();
      for (      IViewReference viewReference : activeReferences) {
        if (managedViewIds.contains(viewReference.getId())) {
          if (perspectiveMemento != null) {
            try {
              IMemento viewMemento=perspectiveMemento.createChild(TAG_VIEW);
              viewMemento.putString("String_Node_Str",viewReference.getId());
              String secondaryId=viewReference.getSecondaryId();
              if (secondaryId != null) {
                viewMemento.putString("String_Node_Str",secondaryId);
              }
              IWorkbenchPart part=viewReference.getPart(false);
              if (part instanceof IViewPart) {
                IViewPart viewPart=(IViewPart)part;
                viewPart.saveState(viewMemento);
              }
            }
 catch (            Exception ex) {
            }
          }
          page.hideView(viewReference);
        }
      }
    }
    saveMementoToFile(memento);
    page.setPerspective(originalPerspective);
  }
}

Fixed version (ground truth):
private void saveAndCloseManagedViews(Set<String> managedViewIds,boolean save){
  IWorkbench workbench=PlatformUI.getWorkbench();
  if (managedArea.getInstalledBundles().size() > 0 && workbench != null) {
    IWorkbenchPage page=null;
    if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() == null) {
      IWorkbenchWindow[] windows=workbench.getWorkbenchWindows();
      for (      IWorkbenchWindow win : windows) {
        page=win.getActivePage();
        if (page != null) {
          break;
        }
      }
    }
 else {
      page=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    }
    if (page == null) {
      return;
    }
    IPerspectiveDescriptor originalPerspective=page.getPerspective();
    XMLMemento memento=XMLMemento.createWriteRoot(TAG_OTE_PRECOMPILED);
    IPerspectiveDescriptor[] pd=page.getOpenPerspectives();
    for (int i=0; i < pd.length; i++) {
      try {
        page.setPerspective(pd[i]);
      }
 catch (      Exception ex) {
      }
      IMemento perspectiveMemento=null;
      try {
        perspectiveMemento=memento.createChild(TAG_PERSPECTIVE);
        perspectiveMemento.putString("String_Node_Str",pd[i].getId());
      }
 catch (      Exception ex) {
      }
      IViewReference[] activeReferences=page.getViewReferences();
      for (      IViewReference viewReference : activeReferences) {
        if (managedViewIds.contains(viewReference.getId())) {
          if (perspectiveMemento != null) {
            try {
              IMemento viewMemento=perspectiveMemento.createChild(TAG_VIEW);
              viewMemento.putString("String_Node_Str",viewReference.getId());
              String secondaryId=viewReference.getSecondaryId();
              if (secondaryId != null) {
                viewMemento.putString("String_Node_Str",secondaryId);
              }
              IWorkbenchPart part=viewReference.getPart(false);
              if (part instanceof IViewPart) {
                IViewPart viewPart=(IViewPart)part;
                viewPart.saveState(viewMemento);
              }
            }
 catch (            Exception ex) {
            }
          }
          page.hideView(viewReference);
        }
      }
    }
    if (save) {
      saveMementoToFile(memento);
    }
    page.setPerspective(originalPerspective);
  }
}