problem with NSmutableArray
- (void)createTileOnScreen:(CGRect)rect
{
myArray = [[NSMutableArray alloc] init];
CGRect bounds = rect;
CGRect myRect = CGRectMake(bounds.origin.x,bounds.origin.y,
(int)(bounds.size.width / 11),
(int)(bounds.size.height / 10));
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 11; j++) {
int index = 10 * i + j;
Liv *myTile = [[Liv alloc] initWithFrame:myR开发者_开发问答ect withIndex:index];
float width = (1.0 / 11);
float height = (1.0 / 10);
[myTile setImageSection:CGRectMake((j / 11),
(1.0 - ((i+1) / 10)),
width,
height)];
[self.rotationView addSubview:myTile];
[myArray addObject:myTile];
[myTile release];
myRect.origin.x += myRect.size.width;
}
myRect.origin.x = bounds.origin.x;
myRect.origin.y = myRect.origin.y + myRect.size.height ;
}
}
#import "Liv.h"
@implementation Liv
@synthesize index;
- (id)initWithFrame:(CGRect)frame withIndex:(int)index_ {
if (self = [super initWithFrame:frame]) {
self.index = index_;
self.backgroundColor = [UIColor blackColor];
fadingAnimationDuration = 2.0;
}
return self;
}
- (id)initWithFrame:(CGRect)frame withIndex:(int)index_ WithColor:(UIColor *)aColor {
if (self = [self initWithFrame:frame withIndex:index_]) {
self.backgroundColor = aColor;
}
return self;
}
when create tile on screen is called a few times, I see that the allocations keep increasing. Can you please let me know how to deallocate myArray without any leaks and also remove it from super view.
Move
myArray = [[NSMutableArray alloc] init];
Out of
- (void)createTileOnScreen:(CGRect)rect
[myArray release];
Will release your tiles fine. But you might want to iterate through the array prior to release and
[[myArray objectAtIndex:index] removeFromSuperview];
How is myArray defined? Is it an instance variable with @propoerty and @synthesize corresponding to it?
it should be
self.myArray = [NSMutableArray array];
with a corresponding
@property(retain) myArray;
in the .h file. Then you will be ok. As per what you have now, every time the
- (void)createTileOnScreen:(CGRect)rect
is called, the old NSMutableArray held by myArray leaks when it is replaced by a new NSMutableArray assigned to myArray.
精彩评论