目标C内存管理 [英] Objective C Memory Management
问题描述
我是Objective C的新手,似乎无法正确获取内存管理代码。我有以下代码:
I'm new to Objective C and can't seem to get the memory management code right. I have the following code:
Media* myMedia = [self.myMediaManager getNextMedia];
self.navigationItem.title = [self.myMediaManager getCategory];
[self.btnImage setImage:myMedia.imageFile forState: UIControlStateNormal];
[self.lblImage setText:myMedia.imageLabel];
//[myMedia release];
如果我取消注释上述行,我的应用程序崩溃了。我实例化myMedia时是否需要做一些特别的事情?
My app crashes if I uncomment the above line. Do I need to do something special when I instantiate myMedia?
编辑:
如果myMediaManager应该发布它什么时候会这样做。这是我的getNextMedia代码:
If myMediaManager is supposed to release it, when would it do that. Here is my code for getNextMedia:
- (Media*) getNextMedia {
DLog(@"Start");
Media* nextMedia = [[Media alloc] init];
[self setNextMediaIndex];
if (self.mediaIndex > -1)
{
nextMedia = [mediaArray objectAtIndex: self.mediaIndex];
}
return nextMedia;
}
EDIT2:我解决了崩溃的问题(我发布了一个我没做过的对象'我自己)。我仍然看到泄漏,似乎无法找到问题所在。
I fixed the crashing issue (I was releasing an object I didn't own). I still see leaks and can't seem to find what the issue is.
推荐答案
只有你拥有的对象可以发布。
如果 new >你可以
,发布
对象 alloc
, copy
, mutableCopy
或首先保留
。
Only objects that you own can be released.
You can release
objects if you new
, alloc
, copy
, mutableCopy
or retain
them first.
由于中没有alloc / copy / retain [ self.myMediaManager getNextMedia];
你无法释放它。
这篇关于目标C内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!