PDA

View Full Version : How release an object if I need to use it somewhere else?




c98928
Apr 16, 2009, 11:38 AM
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:

#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
Apr 16, 2009, 11:46 AM
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
Apr 16, 2009, 12:05 PM
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
Apr 16, 2009, 12:13 PM
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
Apr 17, 2009, 04:00 AM
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