当我调用以下代码时,我正在泄漏内存。我已经尝试了明显的(在我完成后释放对象,并返回一个自动释放的对象)但我收到以下错误:
[CCTouchJoint release]:消息发送到释放实例 0x1dd10f30
在主循环中:
CCTouchJoint *tj = [CCTouchJoint touch:touch withMouseJoint:mouseJoint];
[touchJointList addObject:tj];
[touchJointsHaveNotMoved addObject:tj];
//*If I add [tj release] here we crash
在 CCTouchJoint.h
@interface CCTouchJoint : NSObject
{
@public
b2MouseJoint *mouseJoint;
UITouch *touch;
}
@property (assign) b2MouseJoint *mouseJoint;
@property (nonatomic, retain) UITouch *touch;
在 CCTouchJoint.mm
- (void)dealloc
{
[touch release];
[super dealloc];
}
- (id)initLocal:(UITouch *)_touch withMouseJoint:(b2MouseJoint *)_mouseJoint
{
if ((self = [super init]))
{
self.touch = _touch;
mouseJoint = _mouseJoint;
}
return self;
}
+ (id)touch:(UITouch *)_touch withMouseJoint:(b2MouseJoint *)_mouseJoint
{
return [[self alloc] initLocal:_touch withMouseJoint:_mouseJoint];
//*If I return an autoreleased object here we crash
}
- (void)destroyTouchJoint
{
if (mouseJoint != NULL)
{
mouseJoint->GetBodyA()->GetWorld()->DestroyJoint(mouseJoint);
}
}
关卡退出或重启
-(void)removeAllTouchjoints:(BOOL)release{
//remove touchjoints
for (CCTouchJoint *tj in touchJointList)
{
[tj destroyTouchJoint];
}
for (CCTouchJoint *tj in touchJointsHaveNotMoved)
{
[tj destroyTouchJoint];
}
[touchJointList removeAllObjects];
[touchJointsHaveNotMoved removeAllObjects];
}