Skip to content

Commit f4cc9fe

Browse files
committed
Maybe fixed crashes and memory leak
Signed-off-by: Dan Printzell <[email protected]>
1 parent bc2d82e commit f4cc9fe

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

NoLib.valgrind

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
ignore_unversioned_libs
3+
Memcheck:Leak
4+
...
5+
obj:*/lib*/lib*.so
6+
}
7+
{
8+
ignore_versioned_libs
9+
Memcheck:Leak
10+
...
11+
obj:*/lib*/lib*.so.*
12+
}

src/world/component/component.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// This is a personal academic project. Dear PVS-Studio, please check it.
22
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3-
#include "component.hpp"
3+
#include "component.hpp"

src/world/entity.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77
Entity::Entity(World& world, std::string name) : _world(world), _name(name) {}
88

99
Entity::~Entity() {
10-
for (auto& pair : components) {
11-
try {
12-
components.erase(pair.first);
13-
auto& ents = _world._activeComponents[pair.first];
14-
auto it = std::find(ents.begin(), ents.end(), this);
15-
ents.erase(it);
16-
} catch (std::exception&) {
17-
fprintf(stderr, "MEMORY LEAK!!! Component failed being freed!");
18-
}
10+
auto it = _components.begin();
11+
while (it != _components.end()) {
12+
auto& ents = _world._activeComponents[(*it).first];
13+
ents.erase(std::find(ents.begin(), ents.end(), this));
14+
it++;
1915
}
20-
}
16+
}

src/world/entity.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Entity {
2323

2424
template <typename T, typename... Args, typename std::enable_if<std::is_base_of<Component, T>::value>::type* = nullptr>
2525
T* addComponent(Args... args) {
26-
auto ret = components.insert_or_assign(typeid(T), std::make_unique<T>(args...));
26+
auto ret = _components.insert_or_assign(typeid(T), std::make_unique<T>(args...));
2727

2828
_world._activeComponents[typeid(T)].push_back(this);
2929

@@ -33,17 +33,17 @@ class Entity {
3333
template <typename T, typename std::enable_if<std::is_base_of<Component, T>::value>::type* = nullptr>
3434
T* getComponent() {
3535
T* component = nullptr;
36-
auto it = components.find(typeid(T));
36+
auto it = _components.find(typeid(T));
3737

38-
if (it != components.end())
38+
if (it != _components.end())
3939
component = static_cast<T*>(it->second.get());
4040

4141
return component;
4242
}
4343

4444
template <typename T, typename std::enable_if<std::is_base_of<Component, T>::value>::type* = nullptr>
4545
void removeComponent() {
46-
components.erase(typeid(T));
46+
_components.erase(typeid(T));
4747
auto& ents = _world._activeComponents[typeid(T)];
4848
ents.erase(std::find(ents.begin(), ents.end(), this));
4949
}
@@ -54,13 +54,13 @@ class Entity {
5454
inline std::string& getName() { return _name; }
5555
inline void makeDead() { _dead = true; }
5656
inline bool isDead() { return _dead; }
57-
inline std::map<std::type_index, std::unique_ptr<Component>>& getComponents() { return components; }
57+
inline std::map<std::type_index, std::unique_ptr<Component>>& getComponents() { return _components; }
5858
inline bool& getHide() { return _hide; }
5959

6060
private:
6161
World& _world;
6262
std::string _name;
63-
std::map<std::type_index, std::unique_ptr<Component>> components;
63+
std::map<std::type_index, std::unique_ptr<Component>> _components;
6464

6565
bool _dead = false;
6666
bool _hide = false;

src/world/world.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,4 @@
44

55
World::World() {}
66

7-
World::~World() {
8-
_entities.clear();
9-
_activeComponents.clear();
10-
}
7+
World::~World() {}

src/world/world.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <typeindex>
77

88
class Entity;
9+
class Component;
910

1011
class World {
1112
public:
@@ -26,8 +27,9 @@ class World {
2627

2728
private:
2829
friend class Entity;
29-
std::vector<std::unique_ptr<Entity>> _entities;
30+
friend class Component;
3031
std::map<std::type_index, std::vector<Entity*>> _activeComponents;
32+
std::vector<std::unique_ptr<Entity>> _entities;
3133
};
3234

3335
#include "entity.hpp"

0 commit comments

Comments
 (0)