【问题标题】:Memory Leak warning while addSubView to Current ViewControlleraddSubView 到当前 ViewController 时出现内存泄漏警告
【发布时间】:2011-05-17 12:43:46
【问题描述】:

当我将 subView 添加到当前 ViewController 时,我收到内存泄漏警告..这是我的代码..

     NoOfGolferViewController *objNoOfGolferViewController = [[NoOfGolferViewController alloc]initWithNibName:@"NoOfGolferViewController" bundle:nil];
     [objNoOfGolferViewController setParent:self];
     [objNoOfGolferViewController.view setFrame:CGRectMake(15, 110, 290, 330)];
     [self.view addSubview:objNoOfGolferViewController.view];

当我释放对象时

      [objNoOfGolferViewController release];

应用程序通过给出 EXE_BAD_ACCESS 消息而崩溃。

我该如何解决这个内存泄漏警告?

提前谢谢..

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    您需要保持 objNoOfGolferViewController 对象的活动状态,只要它的视图可见或在当前控制器中使用。似乎最好的解决方案是将其设为当前类的实例变量并在其 dealloc 方法中释放 objNoOfGolferViewController

    【讨论】:

      【解决方案2】:

      您可能在本地声明变量。 而是让它成为全局并在 dealloc 中释放它。 这背后的原因是当您释放对象时,相关的委托方法正在进行中。

      你可以在移除视图后释放对象。

      【讨论】:

        【解决方案3】:

        通常,当您释放已释放的对象时,您会收到此消息。我一直在使用以下方法(在iOS4 - fast context switching 的 SO 上找到)来追踪过去的此类问题:

        #pragma mark - RETAIN DEBUG MAGIC
        // -----------------------------------------------------------------------------
        
        - (id)retain
        {
          NSLog(@"retain \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
          return [super retain];
        }
        - (void)release
        {
          NSLog(@"release \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
          [super release];
        }
        - (id)autorelease
        {
          NSLog(@"autorelease \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
          return [super autorelease];
        }
        

        几天前,我在 SO 使用一个类似的例子写了这篇文章(我在保留和发布一些视图时遇到了问题)。如果您有兴趣,请点击此链接:Understanding iOS Instruments

        祝你好运!

        【讨论】:

          【解决方案4】:

          只需让它自动释放...如下所述

          NoOfGolferViewController *objNoOfGolferViewController = [[[NoOfGolferViewController alloc]initWithNibName:@"NoOfGolferViewController" bundle:nil] autorelease];
          

          【讨论】:

            猜你喜欢
            • 2011-10-23
            • 1970-01-01
            • 1970-01-01
            • 2021-12-27
            • 1970-01-01
            • 1970-01-01
            • 2012-06-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多