代码之家  ›  专栏  ›  技术社区  ›  Alex Gosselin

导致泄漏的核心数据/库存?

  •  1
  • Alex Gosselin  · 技术社区  · 14 年前

    我有一个具体的漏洞,我似乎无法挖掘到底,因为我的搜索总是在一些苹果库结束。一些老兵在处理这件事上的任何帮助都会受到感激。

    这是相关的源代码:(带有注释的泄漏)

    - (void)viewDidLoad {
    //[super viewDidLoad];
    
    NSManagedObjectContext *context = [(iEatAppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];
    
    addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:creator action:@selector(launchCreatorWindow)];
    self.navigationItem.rightBarButtonItem = addButton;
    
    NSFetchRequest *fetReq = [[NSFetchRequest alloc] init];
    [fetReq setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:context]];
    NSError *e = nil;
    
    NSArray *temp = [context executeFetchRequest:fetReq error:&e];  
    //leaking here according to performance tool
    
    self.tableObjects = [[NSMutableArray alloc] initWithArray:temp];
    
    if (e) {
        NSLog(@"%@, %@", e, [e description]);
    }
    [fetReq release];
    }
    

    我试着释放temp,但它与exc-bad-u访问崩溃了,我相信这意味着它已经自动释放了。

    Leaks性能工具表示它是一个类别:cfarray(store deque)event:malloc 还说我的图书馆是负责的。这是堆栈跟踪,8号是我的viewdidLoad帧:

    0 CoreFoundation __CFAllocatorSystemAllocate
    1 CoreFoundation CFAllocatorAllocate
    2 CoreFoundation _CFAllocatorAllocateGC
    3 CoreFoundation _CFArrayReplaceValues
    4 CoreFoundation CFArrayReplaceValues
    5 CoreFoundation -[__NSPlaceholderArray initWithObjects:count:]
    6 CoreFoundation -[NSArray initWithArray:copyItems:]
    7 CoreFoundation -[NSArray initWithArray:]
    

    这一次真的让我陷入困境,任何帮助都非常感谢。

    3 回复  |  直到 14 年前
        1
  •  1
  •   Kendall Helmstetter Gelner    14 年前

    这会导致泄漏:

    self.tableObjects = [[NSMutableArray alloc] initWithArray:temp];
    

    创建一个保留计数为1的数组,然后使用self.tableobjects(如果该属性标记为retain),使该计数达到2。

    然后在dealloc中,当您释放数组时,计数会回到1,而不是0,所以数组永远不会被释放。

    相反,只需这样做:

     self.tableObjects = [NSMutableArray arrayWithArray:temp];
    

    这将返回一个自动释放的数组,因此最终的保留计数将仅为1。

        2
  •  0
  •   Alex Gosselin    14 年前

    答案是我缺少一些基本的东西。 【NSmutablearray alloc】 保留计数=1 self.table对象= 保留计数=2 释放内存 保留计数仍不是0 泄漏。

    对此很抱歉

        3
  •  0
  •   Andy Bowskill    14 年前

    这不是造成泄漏的原因,但我认为无论如何都值得指出:您应该在将addbutton分配给rightbarbutton之后立即释放它,因为它不再需要/使用:

    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];