我在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 ++泄漏,我该怎么做才能跟踪它们?
答案 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