Skip to content

Fix numerous TE memory issues#1164

Open
Nickelony wants to merge 7 commits intodevelopfrom
Nickelony/Fix-Memory-Issues
Open

Fix numerous TE memory issues#1164
Nickelony wants to merge 7 commits intodevelopfrom
Nickelony/Fix-Memory-Issues

Conversation

@Nickelony
Copy link
Copy Markdown
Collaborator

@Nickelony Nickelony commented Mar 22, 2026

This pull request focuses on improving resource management and performance optimizations across several components. The main changes include refining disposal patterns to prevent resource leaks, introducing caching to optimize rendering, and improving hash computation efficiency for textures.

Resource Management Improvements:

  • Refactored disposal patterns for several classes, ensuring proper cleanup of managed and unmanaged resources, including changes to Dispose methods and destructors in ObservableList<T>, GlyphRenderInfo, ImportedGeometryTexture, and various UI panels. This helps prevent memory leaks and double disposal issues. [1] [2] [3] [4] [5] [6] [7] [8] [9]

  • In ImportedGeometryManager, added explicit event handler management for _dataGridViewDataSource.ListChanged to ensure the handler is unsubscribed during disposal, preventing potential memory leaks. [1] [2] [3] [4]

Rendering and Performance Optimizations:

  • Added caching for ghost block vertices (_ghostBlockVertices) and room distance calculations (_roomsDistanceCache) in Panel3D and related draw/collector classes, reducing allocations and improving rendering performance. [1] [2] [3]

Texture and Hashing Enhancements:

  • Optimized the hash computation in WadTexture by directly hashing image dimensions and pixel data using Blake3, eliminating unnecessary intermediate memory allocations for improved performance. [1] [2]

  • Updated ImportedGeometryTexture to implement IDisposable, ensure proper disposal of GPU textures, and guard against use-after-dispose scenarios. [1] [2]

These changes collectively improve the stability, efficiency, and maintainability of the codebase.

@Nickelony Nickelony requested review from Lwmte and Copilot March 22, 2026 14:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR targets reduced memory pressure and improved cleanup by refining disposal patterns and reusing allocations in rendering/editor paths, plus replacing texture hashing with a Blake3-based approach.

Changes:

  • Added/adjusted disposal patterns (including event unsubscription and finalizer suppression) to reduce leaks and redundant finalization.
  • Reduced allocations in geometry clearing, ghost block drawing, and room sorting by reusing buffers/caches.
  • Replaced texture hashing with Blake3 hashing of dimensions + pixel data.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
TombLib/TombLib/Wad/WadTexture.cs Switches texture hashing to Blake3 and removes MemoryStream-based hashing.
TombLib/TombLib/LevelData/RoomGeometry.cs Attempts to preserve list capacity across clears to reduce re-allocations.
TombLib/TombLib/LevelData/ImportedGeometry.cs Makes imported geometry textures disposable and disposes GPU textures when not reused.
TombLib/TombLib/IO/BinaryWriterFast.cs Suppresses finalization after disposal.
TombLib/TombLib.Rendering/Rendering/RenderingFont.cs Fixes dispose guard logic and suppresses finalization.
TombEditor/Controls/Panel3D/Panel3DDrawCollector.cs Reuses a cached distance buffer for sorting rooms to draw.
TombEditor/Controls/Panel3D/Panel3DDraw.cs Reuses a cached vertex array for ghost block rendering.
TombEditor/Controls/Panel3D/Panel3D.cs Adds fields backing the new draw/sort caches.
TombEditor/Controls/ImportedGeometryManager.cs Stores/removes a ListChanged handler to avoid event-handler leaks on dispose.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

TombLib/TombLib.Rendering/Rendering/RenderingFont.cs:323

  • RenderingFont.Dispose() now runs (the _disposed guard was fixed) and it disposes TextureAllocator. However, TextureAllocator is provided by the caller via RenderingFont.Description, and several call sites dispose the allocator separately (often before disposing the font). This makes ownership unclear and can lead to redundant/double disposal or ordering issues. Consider removing allocator disposal from RenderingFont (caller-owned), or update all call sites to treat the allocator as font-owned and dispose only once (and in a consistent order).
        public void Dispose()
        {
            if (_disposed)
                return;
            _disposed = true;

            TextureAllocator?.Dispose();
            GDI.DeleteObject(_gdiFont);

TombLib/TombLib.Rendering/Rendering/RenderingFont.cs:335

  • The finalizer ~RenderingFont() calls Dispose(). Now that Dispose() performs full managed cleanup (including disposing TextureAllocator) and calls GC.SuppressFinalize, the finalizer thread will run that managed disposal path too. Consider switching to the standard Dispose(bool disposing) pattern so the finalizer only releases unmanaged resources (and does not touch potentially already-finalized managed objects), and only Dispose(true) disposes managed fields.
            GC.SuppressFinalize(this);
        }

        ~RenderingFont()
        {
            Dispose();
        }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Nickelony Nickelony added the ready for review The Pull Request is finished and ready for review. label Mar 22, 2026
@Nickelony Nickelony added the bug Something isn't working as intended. label Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as intended. ready for review The Pull Request is finished and ready for review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants