2010-05-24 131 views
2

我有下面的代码中的内存泄漏问题。尽我所知,我不明白为什么问题仍然存在,但在被调用时仍然不释放。我在仪器检测的问题,下面的代码是保持其“卡”类活着,即使它应该已经释放了他们。 欢迎任何帮助。内存泄漏问题

... 
... 
-(id)initDeckWithCardsPicked: (NSMutableArray*)cardsPicked andColors:(NSMutableArray*)cardColors 
    { 
     self = [self init]; 
     if (self != nil) { 
      int count = [cardsPicked count]; 
      for (int i=0; i<count; i++) { 
       int cardNum = [[cardsPicked objectAtIndex:i] integerValue]; 
       Card * card = [[MemoryCard alloc] initWithSerialNumber:cardNum position: CGPointZero color:[cardColors objectAtIndex:i]]; 
       [_cards addObject: card]; 
       [card release]; 
      } 
     } 
     return self;  
     } 

- (id) init 
{ 
    self = [super init]; 
    if (self != nil) { 
     self.bounds = (CGRect){{0,0},[Card cardSize]}; 
     self.cornerRadius = 8; 
     self.backgroundColor = kAlmostInvisibleWhiteColor; 
     self.borderColor = kHighlightColor; 
      self.cards = [NSMutableArray array]; 
     } 
      return self; 
} 
... 
... 

回答

0

当您添加一张卡的NSMutableArray使用addObject的_cards,它会发送retain消息。因此,只要你在记忆中保存_cards,一个指针也会保存在它的每个成分中。只要你dealloc释放磁盘阵列,你这样做的其他地方,你建议立即进行删除DBE细跟你已经张贴在这里什么(假设你的initWithSerialNumber方法返回一个保留的对象)。

+0

我没有通过的dealloc ... [_cards释放]释放阵列; [super dealloc]; ... 但是从我了解类是从其他地方引用,不允许释放? – franz 2010-05-25 00:53:07

1

不看你的代码的其余部分,它很难知道问题出在哪里,但你尝试过用Xcode中静态分析?它对寻找内存泄漏非常重要。

要使用它,请从Build菜单中选择“Build and Analyze”。进一步的细节上Apple's dev website.