There are two questions here. First if I need to create b2BlockAllocator
before Clone
and then delete(where?) after clone? Xcode profiling instrument doesn't show C++ leaks...
b2FixtureDef fixd = fix->fixture;
const b2Shape *shape = fixd.shape;
if(shape->GetType()== b2Shape::e_polygon && flip)
{
b2PolygonShape* ps = (b2PolygonShape*)shape->Clone(new b2BlockAllocator());
for(int i=0;i<ps->m_vertexCount;i++)
{
ps->m_vertices[i].x *= -1;
ps->m_vertices[i].y *= -1;
}
ps->Set(&ps->m_vertices[0], ps->m_vertexCount);
body->CreateFixture(ps, 1.0f);
...
In this code I take cached shape object, clone it, modify vertices, set to calculate normals and assigning it object to body. Question - is this legal?
Update:
-(void) addFixturesToBody:(b2Body*)body forShapeName:(NSString*)shapeName flip:(BOOL)flip
{
BodyDef *so = [shapeObjects_ objectForKey:shapeName];
assert(so);
FixtureDef *fix = so->fixtures;
while(fix)
{
b2FixtureDef fixd = fix->fixture;
const b2Shape *shape = fixd.shape;
if(shape->GetType()== b2Shape::e_polygon && flip)
{
b2BlockAllocator allocator;
b2PolygonShape* ps = (b2PolygonShape*)shape->Clone(&allocator);
for(int i=0;i<ps->m_vertexCount;i++)
{
ps->m_vertices[i].x *= -1;
ps->m_vertices[i].y *= -1;
}
ps->Set(&ps->m_vertices[0], ps->m_vertexCount);
body->CreateFixture(ps, 1.0f);
}
else
{
NSLog(@"noflip...%@", shapeName);
body->CreateFixture(&fix->fixture);
}
fix = fix->next;
}
}
I reply 4 years late while trying to find a similar answer. It seems few people have gone the extra mile with Box2D.
So, you are definitely leaking since
new b2BlockAllocator
doesn't get deleted anywhere, neither by you nor by the Clone function.Just create a local b2BlockAllocator so that it will be destroyed when out-of-scope. And that's it.