Release a NSMutableArray when using ARC

对着背影说爱祢 提交于 2019-12-24 02:05:22

问题


I'm developing an iOS application using latest SDK and ARC.

I have this variable:

NSMutableArray* _previewImageBuffer;

And this method:

- (void)shutdown
{
    [self stop];
    _previewImageBuffer = nil;
}

Is _previewImageBuffer = nil; correct? If I do it, what happens with memory allocated in _previewImageBuffer`? Is this a memory leak?

I want to release this object because I need to release the memory used by it.


回答1:


What you're doing is exactly right. Nilifying an object instance variable under ARC releases the object. Releasing an NSArray, if it causes the NSArray to be deallocated, also releases all its elements.

If you're in doubt or confused about memory management and ARC, you might do well to stop and read up on the facts until you're no longer in doubt or confused. My book has a possibly helpful explanation:

http://www.apeth.com/iOSBook/ch12.html#_memory_management




回答2:


Under ARC this is not a leak. The memory will be released. When learning ARC you should also look at the difference between a strong or weak reference.

You should also check out Apple's official introduction to ARC.



来源:https://stackoverflow.com/questions/15029925/release-a-nsmutablearray-when-using-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!