unsubscribe delegate subcribers in game's quit function and remove destructor calls to unsubscribe; delete ID memory when GL object is deleted; disable recorder because of memory leak

This commit is contained in:
ohsqueezy 2023-06-11 21:49:04 -04:00
parent aaaebc006d
commit 772c5482dd
7 changed files with 26 additions and 10 deletions

View File

@ -457,11 +457,12 @@ External Resources
* [MDN's list of best practices for WebGL](https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/WebGL_best_practices)
* [Emscripten documentation's chapter on optimizing for WebGL](https://emscripten.org/docs/optimizing/Optimizing-WebGL.html)
* [SDL wiki](https://wiki.libsdl.org)
* [Webassembly memory debugging article](https://web.dev/webassembly-memory-debugging/)
Font
----
When initializing a Game object, the framework will attempt to load the font file "BPmono.ttf" from the project root (where the compiled executable is located). If this file isn't found, the program can still run successfully, but the framerate indicator (mapped to CTRL+f by default) will be disabled. The repository contains "BPmono.ttf", so you can create a symlink to the file in the project root if you want to use the framerate indicator.
When initializing a Game object, the framework will attempt to load the font file `BPmono.ttf` from the project root (where the compiled executable is located). If this file isn't found, the program can still run successfully, but the repository contains `BPmono.ttf` if you want to create a symlink to the file in the project root.
License
-------
@ -481,8 +482,8 @@ Included libraries are included under various permissive licenses compatible wit
Contact
-------
* email: frank at shampoo dot ooo
* twitter: https://twitter.com/diskmem
mailbox at shampoo.ooo
https://twitter.com/diskmem
[SDL wiki Android page]: https://wiki.libsdl.org/Android
[SDL docs Android README]: https://github.com/libsdl-org/SDL/blob/main/docs/README-android.md

View File

@ -64,6 +64,9 @@ namespace sb
* Currently, there is no method in the framework for sending custom sb::Delegate::command_event_type events, but they can be
* posted using `SDL_Event` and `SDL_PushEvent`.
*
* The subscribing object must be unsubscribed with Delegate::unsubscribe(const Type*) before it is destroyed, otherwise it will
* be null referenced.
*
* @param func a sb::Node or child of sb::Node object's member class function pointer
* @param obj a pointer to the object
* @param type the SDL_EventType to receive events for

View File

@ -53,7 +53,7 @@ void GLObject::generate(generator_function generator)
/* Set the shared pointer to point to a new GLuint with specified ID value */
void GLObject::id(GLuint id)
{
/* The deleter will be called when the object and all of its copies have gone out of scope */
/* The deleter will be called when the object and all of its copies have gone out of scope */
object_id = std::shared_ptr<GLuint>(new GLuint, deleter);
*object_id = id;
}
@ -91,6 +91,7 @@ void sb::vao_deleter(GLuint* id)
/* not sure why SDL_Log works here on program exit but SDL_LogDebug and SDL_LogInfo don't */
SDL_Log("destroying VAO ID %i", *id);
glDeleteVertexArrays(1, id);
delete id;
}
Buffer::Buffer() : Buffer(GL_INVALID_ENUM) {}
@ -140,6 +141,7 @@ void sb::buffer_deleter(GLuint* id)
/* not sure why SDL_Log works here on program exit but SDL_LogDebug and SDL_LogInfo don't */
SDL_Log("destroying buffer ID %i", *id);
glDeleteBuffers(1, id);
delete id;
}
/* Overload the stream operator to support GLObject. Add a string representation of the object that

View File

@ -39,7 +39,7 @@ Game::Game()
/* If recording is enabled by configuration, activate it. */
if (configuration()["recording"]["enabled"])
{
recorder.animation.play();
// recorder.animation.play();
activate();
}
@ -581,7 +581,7 @@ void Game::frame(float timestamp)
if (last_frame_length < 1000)
{
float timestamp_seconds = timestamp / 1000.0f;
recorder.update(timestamp_seconds);
// recorder.update(timestamp_seconds);
_delegate.dispatch();
audio.update(timestamp_seconds);
input.unsuppress_animation.update(timestamp_seconds);
@ -657,6 +657,12 @@ void Game::handle_quit_event(SDL_Event &event)
void Game::quit()
{
/* Unsubscribe all delegate object's subscribers. */
_delegate.unsubscribe(&audio);
_delegate.unsubscribe(&display);
_delegate.unsubscribe(&input);
_delegate.unsubscribe(this);
if (glcontext != nullptr)
{
SDL_GL_DeleteContext(glcontext);
@ -676,8 +682,6 @@ void Game::quit()
Game::~Game()
{
_delegate.unsubscribe(this);
/* Delete font before quitting the TTF library so the font's shared pointer will successfully call its delete
* function in the TTF library. */
_font.reset();

View File

@ -110,7 +110,7 @@ public:
float frame_time_overflow = 0, last_frame_timestamp, last_frame_count_timestamp;
bool done = false, show_framerate = true, is_gl_context = true;
sb::Display display {this};
Recorder recorder {this};
// Recorder recorder {this};
Input input {this};
Audio audio {this};
std::vector<float> frame_length_history;

View File

@ -162,5 +162,10 @@ Node::~Node()
*
* SDL_Log("Destroying Node %s", get_branch_as_string().c_str());
*/
get_delegate().unsubscribe(this);
/* This will cause problems for Delegates being destroyed but calling unsubscribe, among other undefined behavior
* issues (?)
*
* get_delegate().unsubscribe(this);
*/
}

View File

@ -175,4 +175,5 @@ void sb::texture_deleter(GLuint* id)
/* not sure why SDL_Log works here at program end but SDL_LogDebug and SDL_LogInfo don't */
SDL_Log("destroying texture ID %i", *id);
glDeleteTextures(1, id);
delete id;
}