Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 内存管理示例_Iphone_Objective C - Fatal编程技术网

Iphone 内存管理示例

Iphone 内存管理示例,iphone,objective-c,Iphone,Objective C,这是我的一个类中的一些代码,我想知道我是正确处理内存还是在任何地方泄漏内存 @implementation CardViewController @synthesize playerImage; @synthesize cardLabel; @synthesize card; @synthesize frontView; @synthesize backView; @synthesize nameLabel; @synthesize infoLabel; @synthesize delegate

这是我的一个类中的一些代码,我想知道我是正确处理内存还是在任何地方泄漏内存

@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
@实现CardViewController
@综合玩家形象;
@合成卡片标签;
@综合卡片;
@综合前视;
@综合后视图;
@综合姓名标签;
@综合信息标签;
@综合代表;
-(无效)初始化按钮
{
NSLog(@“initialiseButtons%d”,totalButtons);
intypos=playerImage.frame.origin.y+playerImage.frame.size.height+42;

对于(int i=0;i是的,您应该在每个循环迭代结束时释放该StatView,当您将其插入视图层次结构时。

您应该在将其添加到视图层次结构后立即释放它(通过将新分配的视图作为参数发送
addSubview:
)。这是因为UIView对象保留其子视图


我注意到的另一个问题是:setCard方法应该首先检查新旧卡是否相同,在这种情况下什么也不做。否则,您可能会释放现有卡,然后再次尝试保留它,结果发现它已被解除分配。

您应该尝试XCode中内置的分析器,因为它非常擅长查找这些数据内存泄漏的类型。请检查

  • 在此行之后释放新的
    StatView

    [self.frontView addSubview:sv];

    [sv release];//frontView保留sv

  • dealloc
    中释放声明为
    retain
    copy
    的所有属性。候选属性:playerImage、cardLabel等。发送
    Release
    消息,而不是
    autorelease

    \\[card autorelease];

    [card release];

  • viewDidUnload
    中,释放声明为
    IBOutlet
    的所有属性,并将变量设置为
    nil

    [frontView release],frontView=nil;


  • 我是否可以在帖子中使用标记,以便我发布的代码片段可以很好地显示?是的-输入问题的方向就在那里。使用四个空格或101010按钮。候选属性是什么意思:?保留我猜的属性。