Program: 126762
Project: 126762
Buggy source:
public void unloadUnlockedResources(){
  if (!ProxyRepositoryFactory.getInstance().isFullLogonFinished()) {
    return;
  }
  List<Resource> resourceToUnload=new ArrayList<Resource>();
  List<URI> possibleItemsURItoUnload=new ArrayList<URI>();
  EList<Resource> kaka=xmiResourceManager.resourceSet.getResources();
synchronized (kaka) {
    for (int i=0; i < kaka.size(); i++) {
      Resource resource=kaka.get(i);
      if (resource == null) {
        continue;
      }
      if (!resource.isLoaded()) {
        continue;
      }
      final EList<EObject> contents=resource.getContents();
      for (int j=0; j < contents.size(); j++) {
        EObject object=contents.get(j);
        if (object instanceof Property) {
          if (((Property)object).getItem() instanceof FolderItem) {
            continue;
          }
          Resource itemResource=xmiResourceManager.getItemResource(((Property)object).getItem(),false);
          if (itemResource == null || !itemResource.isLoaded()) {
            continue;
          }
          if (((Property)object).getItem() instanceof RoutineItem) {
            RoutineItem item=(RoutineItem)((Property)object).getItem();
            if (item.isBuiltIn()) {
              continue;
            }
          }
          if (((Property)object).getItem() instanceof SQLPatternItem) {
            SQLPatternItem item=(SQLPatternItem)((Property)object).getItem();
            if (item.isSystem()) {
              continue;
            }
          }
          ERepositoryStatus status=getStatus(((Property)object).getItem());
          if ((status == ERepositoryStatus.LOCK_BY_USER) || (status == ERepositoryStatus.NOT_UP_TO_DATE)) {
            continue;
          }
          resourceToUnload.add(resource);
          if (((Property)object).getItem() != null && ((Property)object).getItem().getParent() != null && (((Property)object).getItem().getParent()) instanceof FolderItem) {
            boolean toKeepInMemorySinceDeleted=false;
            if (((Property)object).getItem().getState().isDeleted()) {
              toKeepInMemorySinceDeleted=true;
            }
 else             if (((Property)object).getItem() instanceof ConnectionItem) {
              Connection connection=((ConnectionItem)((Property)object).getItem()).getConnection();
              boolean haveTableDeleted=false;
              for (              MetadataTable table : ConnectionHelper.getTables(connection)) {
                if (SubItemHelper.isDeleted(table)) {
                  haveTableDeleted=true;
                  break;
                }
              }
              if (!haveTableDeleted && connection != null) {
                QueriesConnection queriesConnection=connection.getQueries();
                if (queriesConnection != null) {
                  for (                  Query query : queriesConnection.getQuery()) {
                    if (SubItemHelper.isDeleted(query)) {
                      haveTableDeleted=true;
                      break;
                    }
                  }
                }
                if (connection instanceof SAPConnection) {
                  SAPConnection sapConn=(SAPConnection)connection;
                  if (!haveTableDeleted) {
                    EList<SAPFunctionUnit> funtions=sapConn.getFuntions();
                    for (                    SAPFunctionUnit unit : funtions) {
                      if (SubItemHelper.isDeleted(unit)) {
                        haveTableDeleted=true;
                        break;
                      }
                    }
                  }
                  if (!haveTableDeleted) {
                    EList<SAPIDocUnit> iDocs=sapConn.getIDocs();
                    for (                    SAPIDocUnit iDoc : iDocs) {
                      if (SubItemHelper.isDeleted(iDoc)) {
                        haveTableDeleted=true;
                        break;
                      }
                    }
                  }
                }
              }
              if (haveTableDeleted) {
                toKeepInMemorySinceDeleted=true;
              }
            }
            if (!toKeepInMemorySinceDeleted) {
              ((FolderItem)((Property)object).getItem().getParent()).getChildren().remove(((Property)object).getItem());
              ((Property)object).getItem().setParent(null);
            }
          }
          possibleItemsURItoUnload.add(xmiResourceManager.getItemResourceURI(resource.getURI()));
        }
      }
    }
    for (int i=0; i < kaka.size(); i++) {
      Resource resource=kaka.get(i);
      if (resource == null) {
        continue;
      }
      final EList<EObject> contents=resource.getContents();
      for (int j=0; j < contents.size(); j++) {
        EObject object=contents.get(j);
        if (!(object instanceof Property)) {
          if (possibleItemsURItoUnload.contains(resource.getURI()) && !resourceToUnload.contains(resource)) {
            resourceToUnload.add(resource);
          }
        }
      }
    }
  }
  AbstractResourceChangesService resChangeService=TDQServiceRegister.getInstance().getResourceChangeService(AbstractResourceChangesService.class);
  for (int i=0; i < resourceToUnload.size(); i++) {
    Resource resource=resourceToUnload.get(i);
    if (resource.isLoaded()) {
      if (resChangeService != null) {
        resChangeService.handleUnload(resource);
      }
      resource.unload();
    }
  }
}

Fixed version (ground truth):
public void unloadUnlockedResources(){
  if (!ProxyRepositoryFactory.getInstance().isFullLogonFinished()) {
    return;
  }
  List<Resource> resourceToUnload=new ArrayList<Resource>();
  List<URI> possibleItemsURItoUnload=new ArrayList<URI>();
  EList<Resource> kaka=xmiResourceManager.resourceSet.getResources();
synchronized (kaka) {
    for (int i=0; i < kaka.size(); i++) {
      Resource resource=kaka.get(i);
      if (resource == null) {
        continue;
      }
      if (!resource.isLoaded()) {
        continue;
      }
      final EList<EObject> contents=resource.getContents();
      for (int j=0; j < contents.size(); j++) {
        EObject object=contents.get(j);
        if (object instanceof Property) {
          if (((Property)object).getItem() instanceof FolderItem) {
            continue;
          }
          Resource itemResource=xmiResourceManager.getItemResource(((Property)object).getItem(),false);
          if (itemResource == null || !itemResource.isLoaded()) {
            continue;
          }
          if (((Property)object).getItem() instanceof RoutineItem) {
            RoutineItem item=(RoutineItem)((Property)object).getItem();
            if (item.isBuiltIn()) {
              continue;
            }
          }
          if (((Property)object).getItem() instanceof SQLPatternItem) {
            SQLPatternItem item=(SQLPatternItem)((Property)object).getItem();
            if (item.isSystem()) {
              continue;
            }
          }
          ERepositoryStatus status=getStatus(((Property)object).getItem());
          if ((status == ERepositoryStatus.LOCK_BY_USER) || (status == ERepositoryStatus.NOT_UP_TO_DATE)) {
            continue;
          }
          resourceToUnload.add(resource);
          if (((Property)object).getItem() != null && ((Property)object).getItem().getParent() != null && (((Property)object).getItem().getParent()) instanceof FolderItem) {
            boolean toKeepInMemorySinceDeleted=false;
            if (((Property)object).getItem().getState().isDeleted()) {
              toKeepInMemorySinceDeleted=true;
            }
 else             if (((Property)object).getItem() instanceof ConnectionItem) {
              Connection connection=((ConnectionItem)((Property)object).getItem()).getConnection();
              boolean haveTableDeleted=false;
              for (              MetadataTable table : ConnectionHelper.getTables(connection)) {
                if (SubItemHelper.isDeleted(table)) {
                  haveTableDeleted=true;
                  break;
                }
              }
              if (!haveTableDeleted && connection != null) {
                QueriesConnection queriesConnection=connection.getQueries();
                if (queriesConnection != null) {
                  for (                  Query query : queriesConnection.getQuery()) {
                    if (SubItemHelper.isDeleted(query)) {
                      haveTableDeleted=true;
                      break;
                    }
                  }
                }
                if (connection instanceof SAPConnection) {
                  SAPConnection sapConn=(SAPConnection)connection;
                  if (!haveTableDeleted) {
                    EList<SAPFunctionUnit> funtions=sapConn.getFuntions();
                    for (                    SAPFunctionUnit unit : funtions) {
                      if (SubItemHelper.isDeleted(unit)) {
                        haveTableDeleted=true;
                        break;
                      }
                    }
                  }
                  if (!haveTableDeleted) {
                    EList<SAPIDocUnit> iDocs=sapConn.getIDocs();
                    for (                    SAPIDocUnit iDoc : iDocs) {
                      if (SubItemHelper.isDeleted(iDoc)) {
                        haveTableDeleted=true;
                        break;
                      }
                    }
                  }
                }
              }
              if (haveTableDeleted) {
                toKeepInMemorySinceDeleted=true;
              }
            }
            if (!toKeepInMemorySinceDeleted) {
              ((FolderItem)((Property)object).getItem().getParent()).getChildren().remove(((Property)object).getItem());
              ((Property)object).getItem().setParent(null);
            }
          }
          possibleItemsURItoUnload.add(xmiResourceManager.getItemResourceURI(resource.getURI(),(((Property)object).getItem()).isNeedVersion(),(((Property)object).getItem()).getFileExtension()));
        }
      }
    }
    for (int i=0; i < kaka.size(); i++) {
      Resource resource=kaka.get(i);
      if (resource == null) {
        continue;
      }
      final EList<EObject> contents=resource.getContents();
      for (int j=0; j < contents.size(); j++) {
        EObject object=contents.get(j);
        if (!(object instanceof Property)) {
          if (possibleItemsURItoUnload.contains(resource.getURI()) && !resourceToUnload.contains(resource)) {
            resourceToUnload.add(resource);
          }
        }
      }
    }
  }
  AbstractResourceChangesService resChangeService=TDQServiceRegister.getInstance().getResourceChangeService(AbstractResourceChangesService.class);
  for (int i=0; i < resourceToUnload.size(); i++) {
    Resource resource=resourceToUnload.get(i);
    if (resource.isLoaded()) {
      if (resChangeService != null) {
        resChangeService.handleUnload(resource);
      }
      resource.unload();
    }
  }
}