crash fix for real this time

This commit is contained in:
Jorijn van der Graaf 2025-11-26 02:52:25 +01:00
commit 99a854d5d1

View file

@ -63,6 +63,7 @@ namespace Crafter {
std::map<const EventListener<T>*, std::chrono::nanoseconds> listenerTimes; std::map<const EventListener<T>*, std::chrono::nanoseconds> listenerTimes;
#endif #endif
protected: protected:
bool deleted = false;
std::map<int, std::vector<EventListener<T>*>> listeners; std::map<int, std::vector<EventListener<T>*>> listeners;
public: public:
@ -78,7 +79,8 @@ namespace Crafter {
} }
EventBase(EventBase&) = delete; EventBase(EventBase&) = delete;
EventBase& operator=(EventBase&) = delete; EventBase& operator=(EventBase&) = delete;
virtual ~EventBase() { ~EventBase() {
deleted = true;
for (const auto& listenerSlice : listeners) { for (const auto& listenerSlice : listeners) {
for (const auto& listener : listenerSlice.second) { for (const auto& listener : listenerSlice.second) {
if(listener) { if(listener) {
@ -170,19 +172,29 @@ namespace Crafter {
this->listenerTimes.clear(); this->listenerTimes.clear();
#endif #endif
for (auto& listenerSlice : this->listeners) { std::vector<std::int_fast32_t> keys;
listenerSlice.second.erase(std::remove(listenerSlice.second.begin(), listenerSlice.second.end(), static_cast<EventListener<void>*>(nullptr)), listenerSlice.second.end()); for (const auto& pair : this->listeners) {
auto sliceCopy = listenerSlice.second; keys.push_back(pair.first);
for (const auto& listener : sliceCopy) { }
#ifdef CRAFTER_TIMING for (std::int_fast32_t key : keys) {
auto start = std::chrono::high_resolution_clock::now(); auto it = this->listeners.find(key);
#endif if (it != this->listeners.end()) {
listener->function(); it->second.erase(std::remove(it->second.begin(), it->second.end(), static_cast<EventListener<void>*>(nullptr)), it->second.end());
#ifdef CRAFTER_TIMING auto sliceCopy = it->second;
auto end = std::chrono::high_resolution_clock::now(); for (const auto& listener : sliceCopy) {
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start); #ifdef CRAFTER_TIMING
this->listenerTimes[listener] = duration; auto start = std::chrono::high_resolution_clock::now();
#endif #endif
listener->function();
if(deleted) {
return;
}
#ifdef CRAFTER_TIMING
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
this->listenerTimes[listener] = duration;
#endif
}
} }
} }
removeEmptyListeners(); removeEmptyListeners();
@ -200,19 +212,29 @@ namespace Crafter {
this->listenerTimes.clear(); this->listenerTimes.clear();
#endif #endif
for (auto& listenerSlice : this->listeners) { std::vector<std::int_fast32_t> keys;
listenerSlice.second.erase(std::remove(listenerSlice.second.begin(), listenerSlice.second.end(), static_cast<EventListener<T>*>(nullptr)), listenerSlice.second.end()); for (const auto& pair : this->listeners) {
auto sliceCopy = listenerSlice.second; keys.push_back(pair.first);
for (const auto& listener : sliceCopy) { }
#ifdef CRAFTER_TIMING for (std::int_fast32_t key : keys) {
auto start = std::chrono::high_resolution_clock::now(); auto it = this->listeners.find(key);
#endif if (it != this->listeners.end()) {
listener->function(data); it->second.erase(std::remove(it->second.begin(), it->second.end(), static_cast<EventListener<T>*>(nullptr)), it->second.end());
#ifdef CRAFTER_TIMING auto sliceCopy = it->second;
auto end = std::chrono::high_resolution_clock::now(); for (const auto& listener : sliceCopy) {
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start); #ifdef CRAFTER_TIMING
this->listenerTimes[listener] = duration; auto start = std::chrono::high_resolution_clock::now();
#endif #endif
listener->function(data);
if(this->deleted) {
return;
}
#ifdef CRAFTER_TIMING
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
this->listenerTimes[listener] = duration;
#endif
}
} }
} }
EventBase<T>::removeEmptyListeners(); EventBase<T>::removeEmptyListeners();