diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-14 17:04:09 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-14 17:04:09 +0200 |
commit | 97e2f08fcc68c83d054dada41c40eaecfc889fb1 (patch) | |
tree | 56533f80a93532d5784d8575c3a97362dcd4ac31 | |
parent | 33bed565c7dc5e419995702b854f618eb13847e5 (diff) |
fix more stability issues
-rw-r--r-- | Museum.cpp | 1 | ||||
-rw-r--r-- | People.cpp | 24 | ||||
-rw-r--r-- | People.h | 11 | ||||
-rw-r--r-- | Tile.cpp | 4 | ||||
-rw-r--r-- | ViewController.cpp | 1 | ||||
-rw-r--r-- | readme.md | 8 |
6 files changed, 30 insertions, 19 deletions
@@ -26,7 +26,6 @@ void Museum::work() { auto next = chrono::steady_clock::now() + this->tick_interval; this->update(); - printf("%lu artists\n", this->people.get_artists().size()); this_thread::sleep_until(next); } } @@ -13,27 +13,32 @@ People::~People() { delete artist; } this->artists.clear(); + this->cleanup(); } void People::add_artist(const ArtistData & data) { - if (this->artists.size() >= 30e3) return; - this->artists.push_back(new Artist(this->museum, data)); + if (this->artist_count >= 10000) return; + + this->artists.push_front(new Artist(this->museum, data)); + this->artist_count++; } void People::remove_artist(Artist & target) { auto it = find(this->artists.begin(), this->artists.end(), &target); if (it == this->artists.end()) return; - this->artists.erase(it); + this->deleted_artists.push_front(*it); + *it = nullptr; + this->artist_count--; } -list<Artist *> People::get_artists() { +forward_list<Artist *> People::get_artists() { return this->artists; } string People::to_string() { string out = ""; - out += stringf("%d artists\n", this->artists.size()); + out += stringf("%d artists\n", this->artist_count); for (Artist * artist : this->artists) { out += stringf("- at (%.2f,%.2f)\n", artist->data.x, artist->data.y); } @@ -41,7 +46,16 @@ string People::to_string() { return out; } +void People::cleanup() { + this->artists.remove(nullptr); + for (Artist * artist : this->deleted_artists) { + delete artist; + } + this->deleted_artists.clear(); +} + void People::update() { + this->cleanup(); for (Artist * artist : this->artists) { if (artist == nullptr) continue; artist->update(); @@ -1,6 +1,6 @@ #pragma once -#include <list> +#include <forward_list> #include <string> #include "Artist.h" @@ -18,12 +18,17 @@ public: void add_artist(const ArtistData & data); void remove_artist(Artist & artist); - std::list<Artist *> get_artists(); + std::forward_list<Artist *> get_artists(); void update(); private: - std::list<Artist *> artists; + std::forward_list<Artist *> deleted_artists; + void cleanup(); + +private: + std::forward_list<Artist *> artists; + size_t artist_count = 0; Museum & museum; }; @@ -35,9 +35,9 @@ Tile * Tile::get_neighbor(int dx, int dy) { int x = this->x + dx; int y = this->y + dy; if (x < 0) return nullptr; - if (x > canvas.data.columns) return nullptr; + if (x >= canvas.data.columns) return nullptr; if (y < 0) return nullptr; - if (y > canvas.data.columns) return nullptr; + if (y >= canvas.data.columns) return nullptr; return &canvas.get_tile(x, y); } diff --git a/ViewController.cpp b/ViewController.cpp index cafccae..269edaa 100644 --- a/ViewController.cpp +++ b/ViewController.cpp @@ -35,6 +35,7 @@ void ViewController::update_tiles(View & view) { void ViewController::update_artists(View & view) { People & people = this->museum.people; for (Artist * artist : people.get_artists()) { + if (artist == nullptr) continue; Rectangle rect = { .x = static_cast<unsigned int>(artist->data.x * scale), .y = static_cast<unsigned int>(artist->data.y * scale), @@ -5,11 +5,3 @@ - artist-artist collision behavior - artist steps on tile behavior -stability issues: - -- `BlueTileBehavior::update_neighbor` causes segfault pretty consistently when - >15k artists exist -- The view very rarely reads artists that were just deleted by the museum - worker thread (set to nullptr first (atomic), delete later) - - |