-
Notifications
You must be signed in to change notification settings - Fork 31
Fix numerous TE memory issues #1164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
11600f7
6b765a0
3d50264
bf342b1
f70582f
4480972
e453a48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,8 @@ public void Dispose() | |
| _ptr = null; | ||
| _startPtr = null; | ||
| _baseStream = null; | ||
|
|
||
| GC.SuppressFinalize(this); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of this code?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It avoids an unnecessary destructor call. if (_handle.IsAllocated)
_handle.Free();Is already called in the |
||
| } | ||
|
|
||
| public Stream BaseStream | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,10 +16,12 @@ | |
|
|
||
| namespace TombLib.LevelData | ||
| { | ||
| public class ImportedGeometryTexture : Texture | ||
| public class ImportedGeometryTexture : Texture, IDisposable | ||
| { | ||
| public Texture2D DirectXTexture { get; private set; } | ||
|
|
||
| private bool _disposed; | ||
|
|
||
Nickelony marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public ImportedGeometryTexture(string absolutePath) | ||
| { | ||
| AbsolutePath = absolutePath; | ||
|
|
@@ -31,22 +33,39 @@ public ImportedGeometryTexture(string absolutePath) | |
| if (SynchronizationContext.Current == null) | ||
| DirectXTexture = TextureLoad.Load(ImportedGeometry.Device, Image); | ||
| else | ||
| SynchronizationContext.Current.Post(unused => // Synchronize DirectX, we can't 'send' because that may deadlock with the level settings reloader | ||
| DirectXTexture = TextureLoad.Load(ImportedGeometry.Device, Image), null); | ||
| SynchronizationContext.Current.Post(unused => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't fully understand what's happening here. This code prevents re-initialization of already disposed texture in race conditions?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is a very rare, but potential order of execution issue, as the posted lambda may happen after the object has been disposed. Extremely rare, but still good to add a defense mechanism just in case. |
||
| if (_disposed) | ||
| return; | ||
|
|
||
| DirectXTexture = TextureLoad.Load(ImportedGeometry.Device, Image); | ||
Nickelony marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, null); | ||
| } | ||
|
|
||
| private ImportedGeometryTexture(ImportedGeometryTexture other) | ||
| { | ||
| DirectXTexture = other.DirectXTexture; | ||
| AbsolutePath = other.AbsolutePath; | ||
| Image = other.Image; | ||
| } | ||
|
|
||
| public void Assign(ImportedGeometryTexture other) | ||
| { | ||
| // Dispose old GPU texture if it differs from the new one. | ||
| if (DirectXTexture != null && DirectXTexture != other.DirectXTexture) | ||
| DirectXTexture.Dispose(); | ||
|
|
||
| AbsolutePath = other.AbsolutePath; | ||
| Image = other.Image; | ||
| DirectXTexture = other.DirectXTexture; | ||
|
|
||
| _disposed = false; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| _disposed = true; | ||
|
|
||
| DirectXTexture?.Dispose(); | ||
| DirectXTexture = null; | ||
| } | ||
Nickelony marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public override Texture Clone() => new ImportedGeometryTexture(this); | ||
Nickelony marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| using System; | ||
| using System.Buffers.Binary; | ||
| using System.IO; | ||
| using TombLib.Graphics; | ||
| using TombLib.Utils; | ||
| using Blake3Hasher = Blake3.Hasher; | ||
|
|
||
| namespace TombLib.Wad | ||
| { | ||
|
|
@@ -20,14 +22,20 @@ public WadTexture(ImageC image) | |
| { | ||
| Image = image; | ||
|
|
||
| using (var ms = new MemoryStream()) | ||
| { | ||
| var writer = new BinaryWriter(ms); | ||
| writer.Write(Image.Size.X); | ||
| writer.Write(Image.Size.Y); | ||
| Image.WriteToStreamRaw(ms); | ||
| Hash = Hash.FromByteArray(ms.ToArray()); | ||
| } | ||
| // Hash image dimensions and pixel data directly without intermediate copies. | ||
| using var hasher = Blake3Hasher.New(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the advantage of
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Less heap allocation, no |
||
| Span<byte> header = stackalloc byte[8]; | ||
| BinaryPrimitives.WriteInt32LittleEndian(header, Image.Size.X); | ||
| BinaryPrimitives.WriteInt32LittleEndian(header.Slice(4), Image.Size.Y); | ||
| hasher.Update(header); | ||
| hasher.Update(Image.ToByteArray()); | ||
Nickelony marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Span<byte> digest = stackalloc byte[32]; | ||
| hasher.Finalize(digest); | ||
|
|
||
| ulong low = BinaryPrimitives.ReadUInt64LittleEndian(digest.Slice(0, 8)); | ||
| ulong high = BinaryPrimitives.ReadUInt64LittleEndian(digest.Slice(8, 8)); | ||
| Hash = new Hash { HashLow = low, HashHigh = high }; | ||
| } | ||
|
|
||
| public override Texture Clone() => this; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain why
Disposemethods were removed for this control. It's important because it's outside of TE codebase.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Dispose()methods weren't removed, they were restructured. The destructor was callingDispose()for no reason, so the code has been simplified.