Library can only be disposed once per application instance
johncbaur opened this issue · comments
I am trying to use this library in an application that allows the user to (repeatedly) open and close an OpenTK window. When the window closed the OpenTK and QuickFont are disposed. The second time the window is created the library fails to render properly because 'SharedState' is pointing to disposed resources. I did a quick fix locally to publicly expose the setter on QFontDrawing SharedState property. When disposing of QuickFont I also set _drawing.SharedState to null. This resolved my issue but perhaps there is a more elegant fix. If this change would be accepted I can send a merge request.
Hi @johncbaur,
Thanks for the bug report!
I agree that the best way to approach this is requiring the user to explicitly dispose the static shared state when an OpenGL context is disposed.
Rather than exposing the setter, I would be happier with a new public static method DisposeStaticState
(or equivalent) that does as the name suggests. To properly clean up the rendering state (debatable if this is needed since you're recreating the rendering context) I would approach it as follows:
- Implement the
IDisposable
interface forQFontSharedState
, following the similar dispose pattern used throughout the library (e.g. dispose methods inQFontDrawing
andQVertexArrayObject
). This would call theGL.DeleteProgram
method on the program ID fromShaderVariables.ShaderProgram
to properly clean up the program object. - Manually dispose static state if required in the
DisposeStaticState
method, something like:
public static void DisposeStaticState()
{
if (_sharedState != null)
{
_sharedState.Dispose();
_sharedState = null;
}
}
The above code might not be entirely correct. If you are happy to implement this then I'll gladly accept a merge request. Otherwise I should have time to implement it within the next few weeks.
Also, happy to hear your thoughts on this approach, and if there might be a cleaner way :)
Hi @johncbaur,
I've pushed a quick fix to the dispose-static-state
branch, creating a new method QFontDrawing.DisposeStaticState()
which disposes the shared render state.
Is your issue fixed by calling DisposeStaticState
after disposing the rest of your QuickFont resources?
Cheers