NSDate内存泄漏问题

时间:2011-07-05 06:37:00

标签: iphone objective-c nstimer

NSDate我有内存泄漏。请查看以下代码。

-(void)myMethods:(NSDate *)currentTime{
    [NSThread detachNewThreadSelector:@selector(mySecondMethods) toTarget:self withObject:nil];
}

-(void) mySecondMethods{
    NSDate * currentDateTime =  [NSDate date];------->memory leak here

    for (Event * event in array) {
        if(![event checkOccur:currentDateTime]){
            return;
         } else {
            [NSThread detachNewThreadSelector:@selector(start) toTarget:event withObject:nil];       
        }
    }
}

2 个答案:

答案 0 :(得分:7)

如果要调用线程中的任何方法,则必须使用自动释放池。

-(void) mySecondMethods
{

   NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
   NSDate * currentDateTime =  [NSDate date];

   for (Event * event in array) {
   if(![event checkOccur:currentDateTime])
   {
      return;
   }
   else{
       [NSThread detachNewThreadSelector:@selector(start) toTarget:event withObject:nil];  

   }
   [pool drain];
}

答案 1 :(得分:0)

-(void) mySecondMethods{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

      NSDate * currentDateTime =  [NSDate date];

    for (Event * event in array) {
        if(![event checkOccur:currentDateTime]){
            return;
        }
        else{
            [NSThread detachNewThreadSelector:@selector(start) toTarget:event withObject:nil];  

        }

    }
    [pool release];
  }