Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

c98928

macrumors member
Original poster
Nov 22, 2008
55
1
I am very confused about when and where to 'release' an object which I created in code block A and will be used later on in code block B.

For instance, in my Controller.m file, I have:
Code:
#import "Controller.h"
.
.
.
- (void) awakeFromNib {
  obj = [[Object alloc] init]; //obj is a property defined in Controller.h
}

- (void) manipulateObj {
  [obj doSomething];
}
.
.
.

Hope you guys know what I'm saying, I know the rule of thumb is to balance up every alloc with release or autorelease. That said, if I release or autorelease obj in the awakeFromNib, I get runtime errors later when my program invokes manipulateObj.

So please enlighten me in this case, how do you apply the rule of thumb, or is it totally fine just leave it unbalanced?
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
Balanced doesn't mean that you always have to have a release in the same method that you create or retain an object. You just need to have a release at some point before the object goes out of scope. Assuming your obj variable is declared in Controller.h, you can release it from anywhere in your Controller.m. Assuming you want obj to be available for the entire life of the Controller, you would release it in the dealloc method of your Controller.
 

c98928

macrumors member
Original poster
Nov 22, 2008
55
1
Thanks for your reply, admanimal

I have another question, say in the function manipulateObj I will need to do something with obj, here do i need to always retain before I use it?
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
well, there is a rule that says a method should be balanced in regard of retains/allocs/copys and releases/autoreleases. but this does not apply to instance variables!

in the example in your original post, you would release "obj" in the dealloc-method of your class.

regarding your second question: it all depends. if obj is, like in your example above, an instance variable (so a property of a class) you should retain it when you first assign it to make sure it's not destroyed while your class still needs it. you do NOT have to retain it in every method you want to use it of course! because as long as you don't release your object inside your class it will not be destroyed since you retained it when you first assigned it. you just have to make sure your object stays alive as long as you need. so you retain it when you declare it and - in most cases - release it in you dealloc method.

memory managment is really difficult, I'm just going through learning it myself. you should really carefully read apple's docs on all things regarding memory managment, that really helps.
also, it helps to post questions with actual code like you did in your first post, that's much more easy to answer than giving you a general explanation of memory managment.
 

c98928

macrumors member
Original poster
Nov 22, 2008
55
1
well, there is a rule that says a method should be balanced in regard of retains/allocs/copys and releases/autoreleases. but this does not apply to instance variables!

in the example in your original post, you would release "obj" in the dealloc-method of your class.

regarding your second question: it all depends. if obj is, like in your example above, an instance variable (so a property of a class) you should retain it when you first assign it to make sure it's not destroyed while your class still needs it. you do NOT have to retain it in every method you want to use it of course! because as long as you don't release your object inside your class it will not be destroyed since you retained it when you first assigned it. you just have to make sure your object stays alive as long as you need. so you retain it when you declare it and - in most cases - release it in you dealloc method.

memory managment is really difficult, I'm just going through learning it myself. you should really carefully read apple's docs on all things regarding memory managment, that really helps.
also, it helps to post questions with actual code like you did in your first post, that's much more easy to answer than giving you a general explanation of memory managment.

thanks for your help, thanks heaps
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.