aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/ComponentManager.hpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-07 10:46:46 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-07 10:46:46 +0100
commit54f4a322e939dcdb62ab9fbf919bf8cb4b6f7b75 (patch)
tree6f740f929ab1f358ae96795f92341565304af3ce /src/crepe/ComponentManager.hpp
parent3a5201961ce31d415042c6273d03e46aed7e6eb8 (diff)
fix script example segmentation fault
Diffstat (limited to 'src/crepe/ComponentManager.hpp')
-rw-r--r--src/crepe/ComponentManager.hpp14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp
index e74f2e9..89ed111 100644
--- a/src/crepe/ComponentManager.hpp
+++ b/src/crepe/ComponentManager.hpp
@@ -30,12 +30,16 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) {
// Create a new component of type T (arguments directly forwarded). The
// constructor must be called by ComponentManager.
- T * instance_pointer = new T(id, forward<Args>(args)...);
- unique_ptr<T> instance = unique_ptr<T>(instance_pointer);
+ T * instance_ptr = new T(id, forward<Args>(args)...);
+ if (instance_ptr == nullptr)
+ throw std::bad_alloc();
+
+ T & instance_ref = *instance_ptr;
+ unique_ptr<T> instance = unique_ptr<T>(instance_ptr);
// Check if the vector size is not greater than get_instances_max
- if (instance->get_instances_max() != -1
- && components[type][id].size() >= instance->get_instances_max()) {
+ int max_instances = instance->get_instances_max();
+ if (max_instances != -1 && components[type][id].size() >= max_instances) {
// TODO: Exception
throw std::runtime_error(
"Exceeded maximum number of instances for this component type");
@@ -44,7 +48,7 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) {
// store its unique_ptr in the vector<>
components[type][id].push_back(std::move(instance));
- return *instance;
+ return instance_ref;
}
template <typename T>