Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.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 sdk的保留计数问题_Iphone_Memory Management_Memory Leaks - Fatal编程技术网

iphone sdk的保留计数问题

iphone sdk的保留计数问题,iphone,memory-management,memory-leaks,Iphone,Memory Management,Memory Leaks,我面临的内存泄漏问题如下: 我正在B类中分配A类对象。//A类对象的保留计数变为1 我正在将对象放置在nsmutablearray中。//类对象的保留计数变为2 在另一个C类中,我获取这个nsmutablearray,获取所有元素,如 for(NSInteger f=0; f< [appDelegate.category_type count]; f++) { [category_type addObject:[appDelegate.category_type objectAtIndex

我面临的内存泄漏问题如下:


  • 我正在B类中分配A类对象。//A类对象的保留计数变为1

  • 我正在将对象放置在nsmutablearray中。//类对象的保留计数变为2

  • 在另一个C类中,我获取这个nsmutablearray,获取所有元素,如

    for(NSInteger f=0; f< [appDelegate.category_type count]; f++)
    {
    [category_type addObject:[appDelegate.category_type objectAtIndex:f]];
    }
    
    //a的保留计数变为2[分配+保留计数为1的提取对象]

  • 我的问题是,我想保留我在tableview中显示的这个数组类别,并想在我在B类中所做的数组中填充新元素后释放它。因此,在添加新元素之前,我要删除B类中的所有元素,并使它们为零,因此对a的引用现在为0。在C班,我在dealloc中发布了一个

    但在Instruments->Leaks中,它显示了C类中这个A类对象的泄漏

    谁能告诉我哪里出了问题


    提前准备好

    我正在B类中分配A类对象。//A类对象的保留计数变为1

    我正在将对象放置在nsmutablearray中。//类对象的保留计数变为2

    在另一个C类中,我获取这个nsmutablearray,在本地nsmutablearray中获取该数组中的所有元素,//RU如何获取???

    释放类B的第一个数组。//本地数组中类A对象的保留计数变为1

    现在在这个类C中,我创建了一个类A的对象,并获取本地nsmutable数组中的元素//本地数组中新类A对象的保留计数变为2[分配+保留计数为1的已获取对象]

    请详细说明这一点,以便容易理解问题

    for(NSInteger f=0; f< [appDelegate.category_type count]; f++)
    {
    [category_type addObject:[appDelegate.category_type objectAtIndex:f]];
    }
    
    NSLog(@"appDelegate.category_type count => %d and category_type count => %d",[appDelegate.category_type count],[category_type count]);
    
    for(NSInteger f=0;f<[appDelegate.category_type count];f++)
    {
    [category_type addObject:[appDelegate.category_type objectAtIndex:f];
    }
    NSLog(@“appDelegate.category_type count=>%d和category_type count=>%d”,[appDelegate.category_type count],[category_type count]);
    

    试用已编辑的代码并查看两个数组的计数。两者都将打印相同的计数,表示两者都有对象即使在释放appDelegate.category\u类型数组之后,对象的保留计数也将是2而不是1。

    我试图理解您的问题,这就是我认为您所说的:

    一,

    二,

    三,

    你需要告诉我们更多关于你在哪里释放对象的信息——在(3)中你说你正在“释放B类的第一个数组”——你能告诉我们代码吗

    看起来您保留了该对象3次,但没有发布任何代码来显示它被释放


    我认为你需要做更多类似的事情:

    1和2。(B类)

    三,。(丙类)

    四,

  • (在B类和C类的解除锁定方法中)

    [类别_类型发布]

  • 在这里,对象a只会被它所在的数组保留—当您不再需要它时,只需释放数组,它就会被释放。您不必担心删除它或自己释放它,这可以通过步骤(1)中的自动释放和释放阵列来解决

    for(NSInteger f=0; f< [appDelegate.category_type count]; f++)
    {
    [category_type addObject:[appDelegate.category_type objectAtIndex:f]];
    }
    
    NSLog(@"appDelegate.category_type count => %d and category_type count => %d",[appDelegate.category_type count],[category_type count]);
    
    A *a = [[A alloc] init];
    
    [category_type addObject:a];
    
    for(NSInteger f=0; f< [appDelegate.category_type count]; f++)
        [category_type addObject:[appDelegate.category_type objectAtIndex:f]];
    
    A *a = [category_type objectAtIndex:i];
    
    A *a = [[[A alloc] init] autorelease];
    
    [category_type release];
    category_type = [NSMutableArray alloc] init];
    [category_type addObject:a];
    
    [category_type release];
    category_type = [[NSArray arrayWithArray:appDelegate.category_type] retain];
    
    A *a = [category_type objectAtIndex:i];