Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - DumpsterDoofus

Pages: [1]
1
DotNet / Re: Is it bad practice to subclass primitives like Text?
« on: July 28, 2020, 04:19:25 am »
Thanks, Ross! After looking at https://github.com/SFML/SFML.Net/blob/2.5/src/SFML.System/ObjectBase.cs#L51 that explanation makes sense. Will make sure to override "Destroy" instead of "Dispose" from now on!

2
DotNet / Is it bad practice to subclass primitives like Text?
« on: December 27, 2019, 09:36:23 pm »
I sometimes write classes like this:

public class FpsText : Text
{
    private readonly MovingAverage _movingAverage = new MovingAverage(30);
    private readonly Clock _clock = new Clock();

    public FpsText(Font font, uint characterSize) : base("CURRENT FPS 60.00", font, characterSize) =>
        Style = Styles.Italic;

    public void Update()
    {
        _movingAverage.Update(_clock.Restart().AsSeconds());
        DisplayedString = $"CURRENT FPS {1/_movingAverage.Average:F2}";
    }

    public new void Dispose()
    {
        base.Dispose();
        _clock.Dispose();
    }
}
 

Since SFML.Graphics.Text.Dispose() is not marked virtual, I have to apply the new keyword to ensure both the text itself and the clock are disposed.

This works fine when it is known that the type is the subtype. However, this breaks when the runtime type is the subtype but the compile-time type is the supertype. For example, if you insert an FpsText into a List<Text> and then iteratively dispose each text, the clock is not disposed, causing a minor memory leak.

The fact that Dispose() is not marked virtual makes me think subclassing built-in types was not intended by the designers.

Is it bad practice to subclass primitive types, like Text? If so, what's the preferred approach? Should I favor composition over inheritance?

Pages: [1]