0

当我调用以下代码时,我正在泄漏内存。我已经尝试了明显的(在我完成后释放对象,并返回一个自动释放的对象)但我收到以下错误:

[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];

}
4

1 回答 1

0

在您的 init 方法中,您使用了不应该这样做的 setter (self.touch =...)。分配给 iVar 并直接保留。

当您返回自动释放对象或向其他方法添加释放时,崩溃(异常消息)是什么?

编辑

好的,我无法使用您提供的代码重现您的错误。所以它必须在班级的其他地方。错误信息是什么?

编辑 2

如果,而不是

CCTouchJoint *tj = [CCTouchJoint touch:touch withMouseJoint:mouseJoint];
[touchJointList addObject:tj];

[touchJointsHaveNotMoved addObject:tj];
//*If I add [tj release] here we crash

你做

CCTouchJoint *tj = [CCTouchJoint alloc] initLocal: touch withMouseJoint:mouseJoint];
[touchJointList addObject:tj];

[touchJointsHaveNotMoved addObject:tj];
[tj release]; //does it crash here?? What message?
于 2012-12-12T17:10:14.227 回答