Skip to content

Commit e5baa84

Browse files
Merge pull request #120 from FishingCactus/fix/memory_singleton
fixes static ref that keeps singletons in memory even when destroyed
2 parents 0b5f4e9 + 87001b3 commit e5baa84

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [2.3.0] - 2024-07-25
4+
### Fixed
5+
- Add `OnDestroy` method to set `Singleton` static instance to null when object is destroyed, this fixes a memory leak when destroying singletons
6+
37
## [2.2.0] - 2024-06-03
48
### Added
59
- Add `SerializableInterface` struct: used to reference objects that implement a specific interface

Runtime/Singleton.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,13 @@ public virtual void Awake()
4848
Destroy( this );
4949
}
5050
}
51+
52+
protected virtual void OnDestroy()
53+
{
54+
if( _instance == this )
55+
{
56+
_instance = null;
57+
}
58+
}
5159
}
5260
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.fishingcactus.common-code",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"displayName": "FC - Common Code",
55
"description": "Code, Extensions and extra features for Fishing Cactus Games.",
66
"unity": "2020.3",

0 commit comments

Comments
 (0)