Skip to content

Commit 57a26a3

Browse files
committed
fix: plug more ressource leaks
Signed-off-by: Jérôme Benoit <[email protected]>
1 parent 2551b77 commit 57a26a3

File tree

4 files changed

+59
-47
lines changed

4 files changed

+59
-47
lines changed

src/queues/abstract-fixed-queue.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,28 @@ export abstract class AbstractFixedQueue<T> implements IFixedQueue<T> {
9999
return undefined
100100
}
101101
const index = this.start
102+
const data = this.nodeArray[index]!.data
103+
this.nodeArray[index] = undefined
102104
++this.start
103105
if (this.start === this.capacity) {
104106
this.start = 0
105107
}
106108
--this.size
107-
return this.nodeArray[index]!.data
109+
return data
108110
}
109111

110112
/** @inheritdoc */
111113
public clear(): void {
114+
if (this.size > 0) {
115+
let index = this.start
116+
for (let i = 0; i < this.size; i++) {
117+
this.nodeArray[index] = undefined
118+
++index
119+
if (index === this.capacity) {
120+
index = 0
121+
}
122+
}
123+
}
112124
this.start = 0
113125
this.size = 0
114126
}

src/queues/priority-queue.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,8 @@ export class PriorityQueue<T> {
112112
let prev: PriorityQueueNode<T> | undefined
113113
while (node != null) {
114114
if (node.delete(data)) {
115-
if (node.empty() && this.head !== this.tail) {
116-
if (node === this.tail) {
117-
this.tail = node.next!
118-
} else {
119-
prev!.next = node.next
120-
if (node === this.head) {
121-
this.head = prev!
122-
}
123-
}
115+
if (node.empty()) {
116+
this.removePriorityQueueNode(node, prev)
124117
}
125118
--this.size
126119
return true
@@ -164,15 +157,8 @@ export class PriorityQueue<T> {
164157
}
165158
const data = targetNode!.dequeue()
166159
--this.size
167-
if (targetNode!.empty() && this.head !== this.tail) {
168-
if (targetNode === this.tail) {
169-
this.tail = this.tail.next!
170-
} else {
171-
prev!.next = targetNode!.next
172-
if (targetNode === this.head) {
173-
this.head = prev!
174-
}
175-
}
160+
if (targetNode!.empty()) {
161+
this.removePriorityQueueNode(targetNode!, prev)
176162
}
177163
return data
178164
}
@@ -231,4 +217,24 @@ export class PriorityQueue<T> {
231217
}
232218
return fixedQueue
233219
}
220+
221+
private removePriorityQueueNode(
222+
nodeToRemove: PriorityQueueNode<T>,
223+
previousNode?: PriorityQueueNode<T>,
224+
): void {
225+
if (this.head === this.tail) {
226+
return
227+
}
228+
229+
if (nodeToRemove === this.tail) {
230+
this.tail = nodeToRemove.next!
231+
} else if (nodeToRemove === this.head) {
232+
this.head = previousNode!
233+
this.head.next = undefined
234+
} else {
235+
previousNode!.next = nodeToRemove.next
236+
}
237+
238+
nodeToRemove.next = undefined
239+
}
234240
}

tests/queues/fixed-priority-queue.test.mjs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('Fixed priority queue test suite', () => {
102102
expect(fixedPriorityQueue.size).toBe(2)
103103
expect(rtItem).toBe(2)
104104
expect(fixedPriorityQueue.nodeArray).toMatchObject([
105-
{ data: 2, priority: -1 },
105+
undefined,
106106
{ data: 1, priority: 0 },
107107
{ data: 3, priority: 0 },
108108
])
@@ -112,8 +112,8 @@ describe('Fixed priority queue test suite', () => {
112112
expect(fixedPriorityQueue.size).toBe(1)
113113
expect(rtItem).toBe(1)
114114
expect(fixedPriorityQueue.nodeArray).toMatchObject([
115-
{ data: 2, priority: -1 },
116-
{ data: 1, priority: 0 },
115+
undefined,
116+
undefined,
117117
{ data: 3, priority: 0 },
118118
])
119119
expect(fixedPriorityQueue.capacity).toBe(queueSize)
@@ -122,19 +122,19 @@ describe('Fixed priority queue test suite', () => {
122122
expect(fixedPriorityQueue.size).toBe(0)
123123
expect(rtItem).toBe(3)
124124
expect(fixedPriorityQueue.nodeArray).toMatchObject([
125-
{ data: 2, priority: -1 },
126-
{ data: 1, priority: 0 },
127-
{ data: 3, priority: 0 },
125+
undefined,
126+
undefined,
127+
undefined,
128128
])
129129
expect(fixedPriorityQueue.capacity).toBe(queueSize)
130130
rtItem = fixedPriorityQueue.dequeue()
131131
expect(fixedPriorityQueue.start).toBe(3)
132132
expect(fixedPriorityQueue.size).toBe(0)
133133
expect(rtItem).toBe(undefined)
134134
expect(fixedPriorityQueue.nodeArray).toMatchObject([
135-
{ data: 2, priority: -1 },
136-
{ data: 1, priority: 0 },
137-
{ data: 3, priority: 0 },
135+
undefined,
136+
undefined,
137+
undefined,
138138
])
139139
expect(fixedPriorityQueue.capacity).toBe(queueSize)
140140
})
@@ -213,7 +213,7 @@ describe('Fixed priority queue test suite', () => {
213213
})
214214

215215
it('Verify clear() behavior', () => {
216-
const fixedPriorityQueue = new FixedPriorityQueue()
216+
const fixedPriorityQueue = new FixedPriorityQueue(2)
217217
fixedPriorityQueue.start = 1
218218
fixedPriorityQueue.size = 2
219219
fixedPriorityQueue.nodeArray = [
@@ -223,9 +223,6 @@ describe('Fixed priority queue test suite', () => {
223223
fixedPriorityQueue.clear()
224224
expect(fixedPriorityQueue.start).toBe(0)
225225
expect(fixedPriorityQueue.size).toBe(0)
226-
expect(fixedPriorityQueue.nodeArray).toMatchObject([
227-
{ data: 2, priority: 0 },
228-
{ data: 3, priority: 0 },
229-
])
226+
expect(fixedPriorityQueue.nodeArray).toStrictEqual([undefined, undefined])
230227
})
231228
})

tests/queues/fixed-queue.test.mjs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('Fixed queue test suite', () => {
100100
expect(fixedQueue.size).toBe(2)
101101
expect(rtItem).toBe(1)
102102
expect(fixedQueue.nodeArray).toMatchObject([
103-
{ data: 1, priority: 0 },
103+
undefined,
104104
{ data: 2, priority: -1 },
105105
{ data: 3, priority: 0 },
106106
])
@@ -110,8 +110,8 @@ describe('Fixed queue test suite', () => {
110110
expect(fixedQueue.size).toBe(1)
111111
expect(rtItem).toBe(2)
112112
expect(fixedQueue.nodeArray).toMatchObject([
113-
{ data: 1, priority: 0 },
114-
{ data: 2, priority: -1 },
113+
undefined,
114+
undefined,
115115
{ data: 3, priority: 0 },
116116
])
117117
expect(fixedQueue.capacity).toBe(queueSize)
@@ -120,19 +120,19 @@ describe('Fixed queue test suite', () => {
120120
expect(fixedQueue.size).toBe(0)
121121
expect(rtItem).toBe(3)
122122
expect(fixedQueue.nodeArray).toMatchObject([
123-
{ data: 1, priority: 0 },
124-
{ data: 2, priority: -1 },
125-
{ data: 3, priority: 0 },
123+
undefined,
124+
undefined,
125+
undefined,
126126
])
127127
expect(fixedQueue.capacity).toBe(queueSize)
128128
rtItem = fixedQueue.dequeue()
129129
expect(fixedQueue.start).toBe(3)
130130
expect(fixedQueue.size).toBe(0)
131131
expect(rtItem).toBe(undefined)
132132
expect(fixedQueue.nodeArray).toMatchObject([
133-
{ data: 1, priority: 0 },
134-
{ data: 2, priority: -1 },
135-
{ data: 3, priority: 0 },
133+
undefined,
134+
undefined,
135+
undefined,
136136
])
137137
expect(fixedQueue.capacity).toBe(queueSize)
138138
})
@@ -209,7 +209,7 @@ describe('Fixed queue test suite', () => {
209209
})
210210

211211
it('Verify clear() behavior', () => {
212-
const fixedQueue = new FixedQueue()
212+
const fixedQueue = new FixedQueue(2)
213213
fixedQueue.start = 1
214214
fixedQueue.size = 2
215215
fixedQueue.nodeArray = [
@@ -219,9 +219,6 @@ describe('Fixed queue test suite', () => {
219219
fixedQueue.clear()
220220
expect(fixedQueue.start).toBe(0)
221221
expect(fixedQueue.size).toBe(0)
222-
expect(fixedQueue.nodeArray).toMatchObject([
223-
{ data: 2, priority: 0 },
224-
{ data: 3, priority: 0 },
225-
])
222+
expect(fixedQueue.nodeArray).toStrictEqual([undefined, undefined])
226223
})
227224
})

0 commit comments

Comments
 (0)