这是我的一个类中的一些代码,我想知道我是否正确地处理了内存,或者我是否在哪里泄漏了内存?
@implementation CardViewController
@synthesize playerImage;
@synthesize cardLabel;
@synthesize card;
@synthesize frontView;
@synthesize backView;
@synthesize nameLabel;
@synthesize infoLabel;
@synthesize delegate;
-(void) initialiseButtons
{
NSLog(@"initialiseButtons %d",totalButtons);
int ypos = playerImage.frame.origin.y + playerImage.frame.size.height + 42;
for(int i=0; i<totalButtons; i++)
{
StatView *sv = [[StatView alloc] initWithYPos:ypos];
sv.tag = 100 + i;
[sv.overlayButton addTarget:self action:@selector(statTapped:)
forControlEvents:UIControlEventTouchUpInside];
sv.overlayButton.tag = 10 + i;
[self.frontView addSubview:sv];
ypos += 26;
}
}
-(IBAction) statTapped:(id) sender
{
UIButton *tappedButton = (UIButton *)sender;
int tag = tappedButton.tag;
if(delegate && [delegate respondsToSelector:@selector(cardTapped:)]) {
[delegate cardTapped:tag-10];
}
}
-(void) viewDidLoad
{
NSLog(@" viewDidLoad CVC");
[self initialiseButtons];
metaData = [[NSArray alloc]
initWithObjects:@"Height",@"Weight",@"Games",@"Attack",@"Defense", nil];
}
-(void) setCard:(Card *)newCard
{
NSLog(@"setCard");
[card release];
card = [newCard retain];
[self updateUI];
}
- (void)dealloc
{
[card autorelease];
[metaData autorelease];
[super dealloc];
}
@end我应该在哪里释放StatView *sv = [StatView alloc :ypos];如果我释放它,每一次循环都会产生问题吗?除此之外,我还能处理剩下的内存吗?
谢谢-Code
发布于 2010-09-15 04:33:43
StatView[self.frontView addSubview:sv];
[sv release]; // frontView retains sv
dealloc中声明为retain或copy的所有属性。候选属性: playerImage、cardLabel等。发送release消息,而不是autorelease\\[card autorelease];
[card release];
viewDidUnload释放所有声明为IBOutlet的属性,并将变量设置为nil[frontView release], frontView = nil;
发布于 2010-09-15 04:25:00
是的,当您将该StatView插入到视图层次结构中时,您应该在每次循环迭代结束时释放它。
发布于 2010-09-15 04:27:49
您应该尝试一下内置到XCode中的分析器,因为它非常擅长发现这些类型的内存泄漏。来个look吧。
https://stackoverflow.com/questions/3712522
复制相似问题