我正在尝试使用Visual Leak Detector查找内存泄漏。
它显示我m_neighbors.push_back(ent);
导致泄漏。
(简短的callstack = NeighborCalculatorDummy - > foreach - > list - > allocate)
我将它用作NeighborCalculatorDummy<Entity *>
,因此pushback应该只在列表中插入指针而不进行任何分配。
所有通过addEntity发出的实体的指针都会在代码中的其他位置删除...
push_back
如何导致泄密?
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;
};
修改
这是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;
});
答案 0 :(得分:1)
在我看来,entity_type
是一个指针(从for_each lambda判断)。
您可能想要使用
NeighborCalculatorDummy<SomeEntity>
而不是
NeighborCalculatorDummy<SomeEntity*>
在您的代码的其他位置(未显示)
当然,lambda拼写不同:
[&](const entity_type& ent){
if(lengthSq(ent.getPosition() - position) <= rSq)
m_neighbors.push_back(ent);
}
也许更多类似的点假定了entity_type的类型需要解除引用。
或者,您可以使用
vector<std::shared_ptr<entity_type> >
而不是当您的实体是多态类型或不可复制/可移动时,这些可能更合适。但是,更改代码可能还需要做更多的工作
答案 1 :(得分:0)
使用此定义,根据entity_type,代码将泄漏或不泄漏。
答案 2 :(得分:0)
我在Entity的父级中省略了虚拟析构函数。这就是为什么推迟它会导致泄密。