Skip to content

refactor: Nullable and Modernization for Core Presentation projects#2654

Merged
Eideren merged 6 commits into
stride3d:masterfrom
Color-Rise:feature/modernize-code-presentation
Mar 1, 2025
Merged

refactor: Nullable and Modernization for Core Presentation projects#2654
Eideren merged 6 commits into
stride3d:masterfrom
Color-Rise:feature/modernize-code-presentation

Conversation

@Kryptos-FR

@Kryptos-FR Kryptos-FR commented Feb 25, 2025

Copy link
Copy Markdown
Member

PR Details

Similar to #2639 and #2643.

Description

Modernize the code, use nullable annotations, try to fix null reference warnings when possible.

Affected projects:

  • Stride.Core.Presentation
  • Stride.Core.Quantum
  • Stride.Core.Quantum.Tests
  • Stride.Core.Presentation.Quantum
  • Stride.Core.Presentation.Quantum.Tests

I purposely left out Stride.Core.Presentation.Wpf as well as Stride.Core.Presentation.Tests as I consider those legacy since we are moving towards using Avalonia in the near future.

Dispose pattern

I also fix the Dispose pattern in some places. It was previously implemented as follow:

class BaseClass : IDisposable
{
    public virtual void Dispose()
    {
        // dispose stuff
    }
}

class DerivedClass : BaseClass
{
    public override void Dispose()
    {
        // dispose other stuff
        base.Dispose();
    }
}

The main issue is that if any derived class does have a finalizer (because it has to release unmanaged resources), it will be broken : finalizer are not supposed to call managed code (included from ancestor classes), as it can lead to unpredictable/undefined behavior. More details https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose and https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/dispose-pattern.

The correct implementation (according to the guidelines linked above) is:

class BaseClass : IDisposable
{
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            // dispose stuff
        }
    }
}

class DerivedClass : BaseClass
{
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            // dispose other stuff
        }
        base.Dispose(disposing);
    }
}

Related issues

#2155
#2156

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My change requires a change to the documentation.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have built and run the editor to try this change out.

@Kryptos-FR Kryptos-FR added the area-Core Issue of the engine unrelated to other defined areas label Feb 25, 2025
@Kryptos-FR Kryptos-FR force-pushed the feature/modernize-code-presentation branch from b7417be to e2303c5 Compare February 26, 2025 08:27
@Kryptos-FR Kryptos-FR marked this pull request as ready for review February 26, 2025 08:55
@Eideren Eideren merged commit c3cd51a into stride3d:master Mar 1, 2025
@Eideren Eideren deleted the feature/modernize-code-presentation branch March 1, 2025 15:50
@Eideren Eideren restored the feature/modernize-code-presentation branch March 1, 2025 15:51
@Eideren

Eideren commented Mar 1, 2025

Copy link
Copy Markdown
Collaborator

Thanks !

@Kryptos-FR Kryptos-FR deleted the feature/modernize-code-presentation branch March 1, 2025 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Core Issue of the engine unrelated to other defined areas

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants