NSDate内存泄漏问题

发布于 2024-11-18 09:04:04 字数 553 浏览 3 评论 0原文

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

I have a memory leak at NSDate. Please review the code below.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剧终人散尽 2024-11-25 09:04:04

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

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

If you are calling any method in a thread you have to use Autorelease Pool.

-(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];
}
怎会甘心 2024-11-25 09:04:04
-(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];
  }
-(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];
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文