Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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 NSDate内存泄漏问题_Iphone_Objective C_Nstimer - Fatal编程技术网

Iphone NSDate内存泄漏问题

Iphone NSDate内存泄漏问题,iphone,objective-c,nstimer,Iphone,Objective C,Nstimer,我在NSDate发生内存泄漏。请检查下面的代码 -(void)myMethods:(NSDate *)currentTime{ [NSThread detachNewThreadSelector:@selector(mySecondMethods) toTarget:self withObject:nil]; } -(void) mySecondMethods{ NSDate * currentDateTime = [NSDate date];------->memor

我在
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];       
        }
    }
}

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

-(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];
}

嘿,谢谢4回复。我添加了nsautorelease池,但它显示nsautorelease池存在泄漏。您需要确保调用排水管。如果“返回”,它击中游泳池将不会排水。
-(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];
  }