开发者

Yet Another IPhone Memory Management Question

So say I'm getting results from my core data request: (this is example code)

On a button press event it does:

NSMutableArray *results = [[myContext executeFetchRequest:request error:&error] mutableCopy];
myObject = (MyObject *)[mutableFetchResults1 objectAtIndex:0]; // assume that there's something in there

Now I want myObject to stick around for awhile in my program... i.e., the information contained in 'myObject' will be used in other events and such. So I have it defined in开发者_如何学C my .h file.

My question is, how do I manage memory in this case. Do I need to 'release' results or myObject at some point? Instruments is saying that this is leaking... But do you see any problems here?


You can just do a retain on the 0 index object from the results like so:

myObject = [(MyObject *)[mutableFetchResults1 objectAtIndex:0] retain];

And then you could release results right after that, myObject would hang around. You would then later need to release myObject as well.


mutableCopy (and copy) returns an object with a retain count of 1 - you are the returned object's owner. so you have to release results at some time.

myObject is retained by the results array. you don't need to explicitly release it as you are not its owner.

if you're only interested in myObject I would retain it and release results as soon as possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜