问题描述
我是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;
}
我修复了崩溃的问题(我释放了一个我不拥有的对象).我仍然看到泄漏,而且似乎找不到问题所在.
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
或retain
,则可以release
个对象.
Only objects that you own can be released.
You can release
objects if you new
, alloc
, copy
, mutableCopy
or retain
them first.
由于[self.myMediaManager getNextMedia];
中没有分配/复制/保留功能,因此无法释放它.
Since there is no alloc/copy/retain in [self.myMediaManager getNextMedia];
you can't release it.
这篇关于Objective-C内存管理:发布时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!