How to create an autorelease NSDictionary in this case?
I have a NSMutableA开发者_运维问答rray instance called entries and I would like to send one of its value (which is a NSDictionary) to a method, but I'm thinking of how to avoid it to leak or properly release it. Here's what I'm doing right now:
NSDictionary *pdata = [self.entries objectAtIndex:indexPath.row];
[self start_download:pdata];
[pdata release]; // <--> Is it ok to do this ?
Thx for helping,
Stephane
you did not alloc, copy or retain pdata so you don't release it
You should absolutely not release pdata
, since you never retained it. -objectAtIndex:
returns a non-owned object. If -start_download:
needs to refer to pdata
after it returns (e.g. if it holds onto it for some asynchronous process) then it should retain pdata
itself, and subsequently it should release pdata
when it's done, but that's orthogonal to the bit of code you pasted.
If you haven't already done so, you should read the Cocoa Memory Management Rules.
Do you have a delegate method for start_download to handle with the download finished? You should release it then.
I would have start_download do a retain on pdata and then release it when it's done. It's always a good idea to have routines that need to hang on to data to a retain and release themselves, that way you don't care how the object got to you and what's going to happen to it after.
精彩评论