Skip to content

Commit

Permalink
Fix reference leak in ComparableTimSort
Browse files Browse the repository at this point in the history
  • Loading branch information
vbousquet committed Mar 18, 2016
1 parent 03ae58e commit 0bbac37
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gdx/src/com/badlogic/gdx/utils/ComparableTimSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ComparableTimSort {

/** Temp storage for merges. */
private Object[] tmp;
private int tmpCount;

/** A stack of pending runs yet to be merged. Run i starts at address base[i] and extends for len[i] elements. It's always true
* (so long as the indices are in bounds) that:
Expand Down Expand Up @@ -86,6 +87,7 @@ public void doSort (Object[] a, int lo, int hi) {
}

this.a = a;
tmpCount = 0;

/** March over the array once, left to right, finding natural runs, extending short natural runs to minRun elements, and
* merging runs to maintain stack invariant. */
Expand Down Expand Up @@ -114,6 +116,11 @@ public void doSort (Object[] a, int lo, int hi) {
if (DEBUG) assert lo == hi;
mergeForceCollapse();
if (DEBUG) assert stackSize == 1;

this.a = null;
Object[] tmp = this.tmp;
for (int i = 0, n = tmpCount; i < n; i++)
tmp[i] = null;
}

/** Creates a TimSort instance to maintain the state of an ongoing sort.
Expand Down Expand Up @@ -761,6 +768,7 @@ private void mergeHi (int base1, int len1, int base2, int len2) {
* @param minCapacity the minimum required capacity of the tmp array
* @return tmp, whether or not it grew */
private Object[] ensureCapacity (int minCapacity) {
tmpCount = Math.max(tmpCount, minCapacity);
if (tmp.length < minCapacity) {
// Compute smallest power of 2 > minCapacity
int newSize = minCapacity;
Expand Down

0 comments on commit 0bbac37

Please sign in to comment.