-
-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
NullReferenceException
in ObjectPool
when reusing all returne…
…d items (#904) * Replaced delegate with Action<T> * Change delegate to Action<T> * Add unit test to confirm issue * Fix NullReferenceException in ObjectPool when reusing all returned items The ObjectPool's Use() method was throwing a NullReferenceException when all items had been returned to the pool and New() was called again. This was due to _tailNode being null in this scenario. Added a null check for _tailNode in Use() method to properly reinitialize the linked list structure when the pool has been emptied and refilled.
- Loading branch information
1 parent
9347374
commit 1f3f1c5
Showing
3 changed files
with
65 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
namespace MonoGame.Extended.Collections | ||
{ | ||
public delegate void ReturnToPoolDelegate(IPoolable poolable); | ||
using System; | ||
|
||
namespace MonoGame.Extended.Collections | ||
{ | ||
public interface IPoolable | ||
{ | ||
IPoolable NextNode { get; set; } | ||
IPoolable PreviousNode { get; set; } | ||
void Initialize(ReturnToPoolDelegate returnDelegate); | ||
void Initialize(Action<IPoolable> returnDelegate); | ||
void Return(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/MonoGame.Extended.Tests/Collections/ObjectPoolTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Craftwork Games. All rights reserved. | ||
// Licensed under the MIT license. | ||
// See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using MonoGame.Extended.Collections; | ||
|
||
namespace MonoGame.Extended.Tests.Collections; | ||
|
||
public class ObjectPoolTests | ||
{ | ||
private class TestPoolable : IPoolable | ||
{ | ||
public Action<IPoolable> ReturnAction { get; private set; } | ||
public IPoolable NextNode { get; set; } | ||
public IPoolable PreviousNode { get; set; } | ||
|
||
public void Initialize(Action<IPoolable> returnAction) | ||
{ | ||
ReturnAction = returnAction; | ||
} | ||
|
||
public void Return() | ||
{ | ||
ReturnAction(this); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ObjectPool_ThrowsNullReferenceException_WhenAllItemsReturnedAndNewCalled() | ||
{ | ||
// Arrange | ||
var pool = new ObjectPool<TestPoolable>(() => new TestPoolable(), 2); | ||
|
||
// Act & Assert | ||
var item1 = pool.New(); | ||
var item2 = pool.New(); | ||
|
||
// Return all items to the pool | ||
item1.Return(); | ||
item2.Return(); | ||
|
||
|
||
var exception = Record.Exception(() => pool.New()); | ||
Assert.Null(exception); | ||
} | ||
} |