问题描述:

I have an object that i'm creating that is causing me some problems with a retain. I'm making an object that runs an animation on repeat forever. When i remove the line that adds the animation and repeat it doesn't retain but when i add the line back in i can tell by using instruments that the object is being retained. Here is the code i'm using. Not sure what the problem is here.

- (id)initWithConveyorId:(int)conveyorId {

self = [super init];

if (self) {

self.conveyorId = conveyorId;

self.zOrder = kZLevelWaterTile;

[self setFlowTexture];

}

return self;

}

- (void)setFlowTexture {

NSMutableArray *animationFrames = [NSMutableArray array];

for (int i=1; i<=kNumberOfDirections; i++) {

[self addAnimationFrameToArray:animationFrames withIndex:i andFlow:kWest];

}

[self runAnimationWithFrames:animationFrames];

}

- (void)addAnimationFrameToArray:(NSMutableArray*)animationFrames withIndex:(int)index andFlow:(NSString*)flow{

NSString* textureName = [NSString stringWithFormat:kTextureFlow, flow];

CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:

[NSString stringWithFormat:kSpriteFrame, textureName, index]];

[animationFrames addObject:frame];

}

- (void)runAnimationWithFrames:(NSMutableArray*)animationFrames {

CCAnimation* flowAnimation = [CCAnimation animationWithSpriteFrames:animationFrames delay:0.1];

CCAction* flowAction = [CCRepeatForever actionWithAction:

[CCAnimate actionWithAnimation:flowAnimation]];

[self runAction:flowAction];

}

any insight to why this is causing a retain would be great! Thanks!

EDIT:

here is the code that creates the conveyor. methods inside this method are convinces methods. I've tested theses and the only problem i get is with adding the animation with CCAction.

- (void)constructRiverWithMaximum:(int)amount {

FFTile* freeTile = [self.matrix getFreeTile];

FFConveyor* currentConveyor = [[FFConveyor alloc] initWithConveyorId:self.conveyorId];

[self replaceTile:freeTile to:currentConveyor];

amount--;

int countConveyorsOnRiver = 1;

// FFConveyor* currentConveyor = firstConveyor;

BOOL hasNext;

FFConveyorFlow directionOfLastTile = FFConveyorFlowNoDirection;

int count = 0;

do {

int x = currentConveyor.columnIndex;

int y = currentConveyor.rowIndex;

NSArray* weights = [self getWeightsByX:x Y:y];

NSArray* probabilities = [self getProbabilitiesByWeights:weights standardWeight:self.standardWeight];

int directionOfCurrentTile = [self defineDirection:probabilities withConveyorCount:countConveyorsOnRiver];

if (directionOfCurrentTile == kConveyorOver) {

hasNext = false;

directionOfCurrentTile = directionOfLastTile;

}

else {

if (amount <= 0){

hasNext = false;

} else {

hasNext = true;

}

}

[self setFlowDirectionFrom:directionOfLastTile to:directionOfCurrentTile forConveyor:currentConveyor];

currentConveyor = [self switchToTurningConveyorIfNeededForConveyorTile:currentConveyor];

//[currentConveyor setFlowTexture];

directionOfLastTile = directionOfCurrentTile;

if (hasNext) {

int conveyorIndex = [self.matrix nextIndexByX:x Y:y direction:directionOfCurrentTile];

FFTile* tile = [self.matrix.tiles objectAtIndex:conveyorIndex];

if ([self.matrix isTileFree:tile]){

currentConveyor = [self createConveyorAtIndex:conveyorIndex];

amount--;

countConveyorsOnRiver++;

} else {

hasNext = false; //end because find another conveyor tile

}

}

count++;

} while (hasNext);

self.conveyorId++;

}

网友答案:

I'm not entirely clear on the question here. But if you're asking why your objects are retained when you add them:

  1. According to your code it's because you are adding them to the mutable array (which will retain the object so long as it's "inside" the array).

  2. I also don't see that you are potentially creating a new objects and then adding them to the array but you are not releasing them after they are created (assuming those are convenience methods for creating them).

  3. Without knowing more about the code outside here, if you're doing memory management manually (i.e. not in ARC) you'll need to manually release your array (and even release all of its objects first to be safe) in the dealloc method.

相关阅读:
Top