3

i need clarification for memory management concepts.

i declare one variable in .h

    @interface RootViewController : UIViewController
    {
         NSMutableArray *objMutableArray;
    }
    @property (nonatomic,retain)   NSMutableArray *objMutableArray;

in .m file

    @implementation RootViewController

    @synthesize objMutableArray=_objMutableArray;

    - (void)viewDidload
    {
         [super viewDidload];

         self.objMutableArray=[[NSMutableArray alloc]init];

        [self.objMutableArray addObject:@"FirstRow"];
        [self.objMutableArray addObject:@"SecondRow"];
        [self.objMutableArray addObject:@"ThirdRow"];
        [self.objMutableArray addObject:@"FourthRow"];
        [self.objMutableArray addObject:@"FifthRow"];
    }

i used self.objMutableArray all places. but when i release memory for that instance i used _objMutableArray.

    - (void)dealloc
    {
         [_objMutableArray release];
         [super dealloc];
    }

actually i confused when i release memory for that instance. please tell me i did correct or i must release "objMutableArray" object.

Ravi
  • 888
  • 6
  • 24
  • 1
    Meta-meta-meta? Memory management management? Or do you only want to ask "how to manage memory"? –  Jul 18 '13 at 07:06
  • i need clarification in above situation, which object memory will release in deallocation – Ravi Jul 18 '13 at 07:09
  • 1
    @PartiallyFinite Let him use whichever technology he wants to use. You see, he wants to understand manual memory management. **Be glad that a beginner wants to understand manual memory management.** –  Jul 18 '13 at 07:22
  • @PartiallyFinite Exactly. Even better, one should learn C first and only then should one continue with Objective-C and iOS. –  Jul 18 '13 at 07:26

3 Answers3

2

You seem to be using manual memory management and not ARC. Which is fine, but you got it wrong.

please tell me i did correct or i must release "objMutableArray" object.

Of course you have to release it because you created it using alloc. But how you did it is not correct. You are leaking memory because in the viewDidUnload method (I suppose that should be viewDidLoad instead, shouldn't it!?) you are assigning to a retain property - your object will have a reference count of two (one because of + alloc, one because of (retain)).

Now when you are releasing it in - dealloc, it will still have a reference count of one, so your class doesn't dispose of its ownership, hence the memory leak.

Solution:

You can use either the property or the instance variable. Don't mix the two. Approach #1:

_objMutableArray = [[NSMutableArray alloc] init];

// ...

[_objMutableArray release];

Approach #2:

self.objMutableArray = [[[NSMutableArray alloc] init] autorelease];

// ...

self.objMutableArray = nil;
0

Going through your code

1) Did you mean viewDidLoad instead of viewDidUnload

- (void)viewDidLoad
{
     [super viewDidLoad];

     self.objMutableArray=[[NSMutableArray alloc]init];
     ......
     ......

}

2) You don't need to alloc/init since you are retaining the array

Use

self.objMutableArray = [NSMutableArray array];

instead of

self.objMutableArray=[[NSMutableArray alloc]init];

3) Now your actual question, When releasing in dealloc

I think this question is already answered.

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • thank you krishna, my actual question is when i write synthesize for objMutableArray object, i declared _ objMutableArray. and i used self. objMutableArray. now which object memory i should release – Ravi Jul 18 '13 at 07:17
0

Shouldn't this viewDidUnload be viewDidLoad. Since you have written @synthesize objMutableArray=_objMutableArray;, so from there on if you want to access this property directly without making use of setters and getters. You will have to use _objMutableArray. That line in dealloc [_objMutableArray release]; will decrease the objects retain count by one, such that if the resultant retain count is 0, the the object will be released.

devluv
  • 2,007
  • 13
  • 13