Skip to content

Commit

Permalink
more attention to disposing unmanaged memory
Browse files Browse the repository at this point in the history
  • Loading branch information
notgiven688 committed Feb 22, 2023
1 parent da192f2 commit ce5066f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/Drawables/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public void Load()
// nothing to do
}

public void Dispose()
{
// nothing to do
}

public void Update(FrameEventArgs e)
{
KeyboardState ks = Showcase.Current.KeyState;
Expand Down
7 changes: 7 additions & 0 deletions src/Drawables/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public void SetProperties(Vector3 pointA, Vector3 pointB)
translation = Matrix4.CreateTranslation(pointB);
sphereB.WorldMatrix = scale * translation;
}

public void Dispose()
{
tube.Dispose();
sphereA.Dispose();
sphereB.Dispose();
}
}

public class Tube : Primitive
Expand Down
3 changes: 2 additions & 1 deletion src/Drawables/Primitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace GJKEPADemo
{
public class Primitive : IDrawableComponent, IDisposable
public class Primitive : IDrawableComponent
{
private BufferHandle VBO, EBO;
private VertexArrayHandle VAO;
Expand Down Expand Up @@ -114,6 +114,7 @@ public virtual void Update(FrameEventArgs e)

public virtual void Dispose()
{
GL.DeleteVertexArray(VAO);
GL.DeleteBuffer(EBO);
GL.DeleteBuffer(VBO);
}
Expand Down
12 changes: 10 additions & 2 deletions src/Drawables/Showcase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Showcase : IDrawableComponent

public MainWindow Window { get; private set; }

private List<IDrawableComponent> drawableComponents = new List<IDrawableComponent>();
private List<IDrawableComponent> drawableComponents = new ();

public ImplicitShape PrimitiveLeft { private set; get; }
public ImplicitShape PrimitiveRight { private set; get; }
Expand All @@ -51,7 +51,7 @@ public class Showcase : IDrawableComponent
private bool autorotate = true;
private bool advancerotation = false;

private Random random = new Random();
private Random random = new ();

public List<Type> AllShapes { get; private set; }

Expand Down Expand Up @@ -203,5 +203,13 @@ public void Update(FrameEventArgs e)

foreach (var component in drawableComponents) component.Update(e);
}

public void Dispose()
{
foreach (var component in drawableComponents)
{
component.Dispose();
}
}
}
}
9 changes: 9 additions & 0 deletions src/Drawables/TextOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ public void Update(FrameEventArgs e)
{
// nothing to do
}

public void Dispose()
{
shader.Dispose();
GL.DeleteTexture(texture);
GL.DeleteVertexArray(VAO);
GL.DeleteBuffer(VBO);
GL.DeleteBuffer(EBO);
}
}

public static class Monospace12
Expand Down
8 changes: 7 additions & 1 deletion src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace GJKEPADemo
{
public interface IDrawableComponent
public interface IDrawableComponent : IDisposable
{
public void Draw(FrameEventArgs e);
public void Update(FrameEventArgs e);
Expand All @@ -50,6 +50,12 @@ protected override void OnLoad()
showcase.Load();
}

protected override void OnUnload()
{
showcase.Dispose();
base.OnUnload();
}

protected override void OnResize(ResizeEventArgs e)
{
base.OnResize(e);
Expand Down

0 comments on commit ce5066f

Please sign in to comment.