Reputation: 7834
I am trying to find memory leak with Visual Leak Detector.
It shows me m_neighbors.push_back(ent);
causes leak.
(brief callstack = NeighborCalculatorDummy -> foreach -> list -> allocate)
I use it as NeighborCalculatorDummy<Entity *>
, so pushback should just insert pointer in list without any allocation.
All pointers to entities which come through addEntity are deleted elsewhere in code...
How is it possible for push_back
to cause a leak?
template <typename entity_type>
class NeighborCalculatorDummy
{
public:
inline void addEntity(const entity_type & entity)
{
m_entities.push_back(entity);
}
void calculateNeighbors(const vector_type & position, flt32 radius)
{
flt32 rSq = radius*radius;
m_neighbors.clear();
std::for_each(m_entities.begin(), m_entities.end(), [&](entity_type ent){
if(lengthSq(ent->getPosition() - position) <= rSq)
m_neighbors.push_back(ent);
});
}
private:
std::vector<entity_type> m_entities;
std::list<entity_type> m_neighbors;
};
edit
here is the code around NeighborCalculator
//#1
std::list<Vehicle *> vehicles;
vehicles.push_back(new Vehicle);
vehicles.push_back(new Vehicle);
vehicles.push_back(new Vehicle);
//#2
NeighborCalculatorDummy<Vehicle *> neighborCalculator = new NeighborCalculatorDummy<Vehicle *>();
std::for_each(vehicles.begin(), vehicles.end(), [&](Vehicle * vehicle){
neighborCalculator->addEntity(vehicle);
});
//#3 impl of addEntity
template <typename entity_type>
void NeighborCalculatorDummy<entity_type>::addEntity(const entity_type & entity)
{
...
m_entities.push_back(entity); //m_entities is - std::vector<Vehicle *>
}
//#4 end of program
delete neighborCalculator;
std::for_each(vehicles.begin(), vehicles.end(), [&](Vehicle * vehicle){
delete vehicle;
});
Upvotes: 0
Views: 908
Reputation: 7834
I have omitted virtual destructor in Entity's parent. That is why pushbacking it caused a leak.
Upvotes: 0
Reputation: 10828
With this definition, depending on entity_type, the code will leak or not.
Upvotes: 0
Reputation: 393457
It seems to me that entity_type
is a pointer (judging from the for_each lambda).
You probably wanted to use
NeighborCalculatorDummy<SomeEntity>
instead of
NeighborCalculatorDummy<SomeEntity*>
in some other place of your code (not shown)
Of course the lambda would then be spelled differently:
[&](const entity_type& ent){
if(lengthSq(ent.getPosition() - position) <= rSq)
m_neighbors.push_back(ent);
}
and perhaps more similar spots that assumed the type of entity_type needed dereferencing.
Alternatively, you could use
vector<std::shared_ptr<entity_type> >
insteadThese might be more appropriate when your entities are polymorphic types or non-copyables/movables. However, it is also likely more work to change you code around
Upvotes: 1