views:

237

answers:

2

Hi all,

I'm facing a memory leak problem which is like this:

  1. I'm allocating an object of class A in class B. // RETAIN COUNT OF CLASS A OBJECT BECOMES 1

  2. I'm placing the object in an nsmutablearray. // RETAIN COUNT OF CLASS A OBJECT BECOMES 2

  3. In an another class C, I'm grabbing this nsmutablearray, fetching all the elements like

    for(NSInteger f=0; f< [appDelegate.category_type count]; f++)
    {
    [category_type addObject:[appDelegate.category_type objectAtIndex:f]];
    }
    

    in a local nsmutablearray, releasing this first array of class B i.e. appDelegate.category_type.
    // RETAIN COUNT OF CLASS A OBJECTS IN LOCAL ARRAY [category_type] BECOMES 1

  4. Now in this class C, I'm creating an object of class A and fetching the elements in local nsmutablearray like:

     A *a = [[A alloc]init]; 
     a = [category_type objectAtIndex:i];       
    

    //RETAIN COUNT OF a BECOMES 2 [ALLOCATION + FETCHED OBJECT WITH RETAIN COUNT 1]

My question is, I want to retain this array category_type which I'm displaying in tableview, and want to release it after new elements are filled in the array which I'm doing in class B. So before adding new elements, I'm removing all the elements in class B and making them nil so the references to a are now 0. And in class C I'm releasing a in dealloc.

But in Instruments->Leaks it's showing me leak for this class A object in class C.

Can anybody please tell me wheather where I'm going wrong.

Thanx in advance.

A: 

I'm allocating an object of class A in class B. // RETAIN COUNT OF CLASS A OBJECT BECOMES 1

I'm placing the object in an nsmutablearray. // RETAIN COUNT OF CLASS A OBJECT BECOMES 2

In an another class C, I'm grabbing this nsmutablearray, fetching all the elements in that array in a local nsmutablearray, //HOW R U FETCHING???

releasing this first array of class B. // RETAIN COUNT OF CLASS A OBJECTS IN LOCAL ARRAY BECOMES 1

Now in this class C, I'm creating an object of class A and fetching the elements in local nsmutable array. //RETAIN COUNT OF NEW CLASS A OBJECT IN LOCAL ARRAY BECOMES 2 [ALLOCATION + FETCHED OBJECT WITH RETAIN COUNT 1]

please elaborate this point so that it will be easy to understand the problem.

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]);

Tryout the edited code and see the count of both the arrays. both will print the same count, means both have the object. Even after releasing the appDelegate.category_type array, your object's retain count will be 2 not 1.

Manjunath
Please check my edited question.
neha
Check the edited answer.
Manjunath
Thanx Manjunath.. I'm planning to check this tonight..
neha
How did you solve your problem?
Manjunath
A: 

I'm trying to understand your problem, this is what I think you're saying :

1.

A *a = [[A alloc] init];

2.

[category_type addObject:a];

3.

for(NSInteger f=0; f< [appDelegate.category_type count]; f++)
    [category_type addObject:[appDelegate.category_type objectAtIndex:f]];

4.

A *a = [category_type objectAtIndex:i];

You need to tell us more about where you're releasing objects - in (3) you say you are 'releasing this first array of class B' - can you show us the code?

It looks like you're retaining the object 3 times but haven't posted any code to show it getting released at all.


I think that you need to do something more like this :

1 & 2. (in class B)

A *a = [[[A alloc] init] autorelease];

[category_type release];
category_type = [NSMutableArray alloc] init];
[category_type addObject:a];

3. (in class C)

[category_type release];
category_type = [[NSArray arrayWithArray:appDelegate.category_type] retain];

4.

A *a = [category_type objectAtIndex:i];
  1. (in both class B and C's dealloc method)

    [category_type release];

Here, object a is only ever being retained by the arrays that it's in - when you don't want it any more just release the array and it gets released. You don't need to worry about removing it or releasing it yourself, that gets taken care of by the autorelease in step (1) and releasing the arrays.

deanWombourne
Thanx for trying to help me deanWombourne.. I'll check this tonight..
neha