From b5d6e53e50f1b39689d4ddaf8040fa46d74dfc91 Mon Sep 17 00:00:00 2001 From: Florian Kirmaier Date: Tue, 27 Apr 2021 14:56:38 +0200 Subject: [PATCH] Avoid memory leak when PropertyComparator is reused This commit fixes a potential memory leak, since PropertyComparator previously kept an indirect reference to the value it last compared. Closes gh-26869 --- .../beans/support/PropertyComparator.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/support/PropertyComparator.java b/spring-beans/src/main/java/org/springframework/beans/support/PropertyComparator.java index 43e927f2830..519ead57987 100644 --- a/spring-beans/src/main/java/org/springframework/beans/support/PropertyComparator.java +++ b/spring-beans/src/main/java/org/springframework/beans/support/PropertyComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,8 +44,6 @@ public class PropertyComparator implements Comparator { private final SortDefinition sortDefinition; - private final BeanWrapperImpl beanWrapper = new BeanWrapperImpl(false); - /** * Create a new PropertyComparator for the given SortDefinition. @@ -115,8 +113,9 @@ public class PropertyComparator implements Comparator { // (similar to JSTL EL). If the property doesn't exist in the // first place, let the exception through. try { - this.beanWrapper.setWrappedInstance(obj); - return this.beanWrapper.getPropertyValue(this.sortDefinition.getProperty()); + BeanWrapperImpl beanWrapper = new BeanWrapperImpl(false); + beanWrapper.setWrappedInstance(obj); + return beanWrapper.getPropertyValue(this.sortDefinition.getProperty()); } catch (BeansException ex) { logger.debug("PropertyComparator could not access property - treating as null for sorting", ex);