【问题标题】:How to avoid leaking in iPhone application?如何避免在 iPhone 应用程序中泄漏?
【发布时间】:2011-10-10 11:33:47
【问题描述】:

我正在获取崩溃日志:

2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fbdef0 of class NSURL autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x1462e38 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x1462e38 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb32b0 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.235 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fc04e0 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.235 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5f98960 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.235 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fa9c70 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.550 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fbfbb0 of class NSHTTPURLResponse autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.550 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb5840 of class __NSCFData autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.550 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb1400 of class __NSArrayM autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5f83e70 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fbd480 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb31b0 of class NSPathStore2 autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fa9aa0 of class NSPathStore2 autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fa6110 of class __NSArrayI autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.552 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb9700 of class NSCFString autoreleased with no pool in place - just leaking

任何机构都可以帮助我避免崩溃吗?

【问题讨论】:

    标签: iphone memory-leaks crash nsautoreleasepool


    【解决方案1】:

    您很可能会看到这种情况,因为您在没有自动释放池的线程上执行代码。 Apple 的所有 API 都大量使用自动释放池,因此在整个线程中封装一个非常重要。这方面的一个例子如下:

    - (void)myThreadMethod:(id)anObject {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        NSLog(@"This is some objective-c code...");
        [pool drain];
    }
    

    [游泳池排水]部分非常重要。如果没有该 sn-p 代码,所有在线程生命周期内自动释放的对象都将被泄露。

    【讨论】:

    • 我正在使用带有 ARC 的 xcode 4.2。在那个 NSAutoreleasePool 已被弃用。我的应用程序没有崩溃,但在日志中相同的日志即将到来。它对我的应用程序有害吗?请帮帮我。
    【解决方案2】:

    设置NSAutoreleasePool 的实例。查看NSAutoreleasePool Class Reference 了解详细信息和示例。

    您可能还想浏览Memory Management Programming Guide 以了解设置它的重要性以及autorelease 的实际作用。

    我还发现这篇帖子的讨论很有帮助:How does the NSAutoreleasePool autorelease pool work?

    【讨论】:

      【解决方案3】:

      来自 Apple 文档link

      NSAutoreleasePool 类用于支持 Cocoa 的 引用计数内存管理系统。自动释放池存储 当池本身被发送释放消息的对象 倒掉了。

      重要提示:如果您使用自动引用计数 (ARC),则无法 直接使用自动释放池。相反,您使用 @autoreleasepool 块。例如,代替:

       NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
      // Code benefitting from a local autorelease pool. 
      [pool release];
      

      你会写:

       @autoreleasepool {
           // Code benefitting from a local autorelease pool. 
      }
      

      @autoreleasepool 块比使用 NSAutoreleasePool 直接;即使您不使用它们也可以使用它们 使用 ARC。

      【讨论】:

        猜你喜欢
        • 2018-08-29
        • 2011-12-17
        • 2023-03-16
        • 1970-01-01
        • 2018-02-02
        • 1970-01-01
        • 2014-06-29
        • 1970-01-01
        • 2020-05-13
        相关资源
        最近更新 更多