Re: Memory management about async object
Re: Memory management about async object
- Subject: Re: Memory management about async object
- From: James Bucanek <email@hidden>
- Date: Wed, 29 Dec 2010 09:54:23 -0700
Answering myself...
James Bucanek <mailto:email@hidden> wrote (Tuesday,
December 28, 2010 8:36 AM -0700):
ico <mailto:email@hidden> wrote (Tuesday, December 28, 2010 11:18 PM +0800):
My question is, when should I release the myHandler? Should I release it in
the myCallback, but after
myCallback method finishes, it will return point 1 indicated as above, and
that myHandler instance is just
released.
As a general rule, I would avoid releasing |self| since (conceptually) an
object does not own itself.
This is excellent advice. I should have paid attention to it.
The answer to your question is, I think, simple: Follow good memory management
procedures. Retain objects until you no longer need to reference them, and
then release them.
More excellent advice. I'm just a fountain of wisdom.
Having said that, if your connectionDidFinishLoading: does all of the post
processing, and after it returns there are no more references to the
DataHandler object, then it would be appropriate to release/autorelease it in
the callback.
OK, when I actually thought about the problem, I realized that
my earlier advice is crap.
If your high-level code that sets up your DataHandler object
starts it, and then forgets about it, then it should release it
when it's done with it (memory rule #1):
- (void) loadData {
DataHandler *myHandler = [[DataHandler alloc] init];
// set this view controller itself as a delegate, when the
data loading
finished, myHandler will call its callback method
myHandler.delegate = self;
[myHandler startAsyncLoading];
[myHandler release]; <--------------- done with myHandler
The memory management rules always apply. The -startAsyncLoading
message must start some background operation. Since this
"something" plans to send myHandler a message, then it *must*
retain myHandler until that message is sent. So, once started,
myHandler must be retained by whatever agent is performing the
asynchronous load until that load is done. If it isn't, then
that's a bug.
--
James Bucanek
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden