无法使用仪器跟踪C ++泄漏

时间:2013-10-01 10:17:49

标签: c++ ios xcode memory-leaks cocos2d-iphone

我在Objective-c应用程序中使用C ++并在场景中进行以下分配:

@interface Game1PlayerLayer : CCLayer {

    b2World *_world;
    GLESDebugDraw *_debugDraw;
    MyContactListener *_contactListener;
}

+(CCScene *) scene;

@end

在实施中我分配:

_world = new b2World(gravity);
_debugDraw = new GLESDebugDraw(PTM_RATIO);
_contactListener = new MyContactListener();

当然我必须用

解除分配
delete _world;
delete _debugDraw;
delete _contactListener;

但如果我没有删除它们,Xcode Leaks仪器不会显示那些泄漏。我当然检查了Game1PlayerLayer的重新分配。泄漏仪器设置为1秒快照。如果Xcode能够显示C ++泄漏,我该怎么做才能跟踪它们?

1 个答案:

答案 0 :(得分:0)

如何避免整个问题并改为使用std::shared_ptr

@interface Game1PlayerLayer : CCLayer {

    std::shared_ptr<b2World> _world;
    std::shared_ptr<GLESDebugDraw> _debugDraw;
    std::shared_ptr<MyContactListener> _contactListener;
}


_world.reset(new b2World(gravity));
_debugDraw.reset(new GLESDebugDraw(PTM_RATIO));
_contactListener.reset(new MyContactListener());


#if 0
delete _world;
delete _debugDraw;
delete _contactListener;
#endif