博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Why should a self-implemented getter retain and autorelease the returned object
阅读量:5294 次
发布时间:2019-06-14

本文共 2734 字,大约阅读时间需要 9 分钟。

-(id)getMyInstance    {
return myInstanceVar ;}

or

-(id)getMyInstance{
return[[myInstanceVar retain] autorelease];}

What's the difference ? The second one allows the caller to get an instance variable of a container object, dispose of the container and continue to play with the instance variable until the next release of the current autoreleased pool, without being hurt by the release of the instance variable indirectly generated by the release of its container:

aLocalVar =[aContainer getAnInstanceVar];[aContainer release];doSomething(aLocalVar);

If the "get" is implemented in the first form, you should write:

aLocalVar =[[aContainer getAnInstanceVar] retain];[aContainer release];doSomething(aLocalVar);[aLovalVar release];

The first form is a little bit more efficent in term of code execution speed. However, if you are writing frameworks to be used by others, maybe the second version should be recommanded: it makes life a little bit easier to people using your framework: they don't have to think too much about what they are doing…;) If you choose the first style version, state it clearly in your documentation… Whatever way you will be choosing, remember that changing from version 1 to version 2 is save for client code, when going back from version 2 to version 1 will break existing client code…

 

 

 

 

 

Compare this code

return[[title retain] release];// releases immediately

with this

return[[title retain] autorelease];// releases at end of current run loop (or if autorelease pool is drained earlier)

The second one guarantees that a client will have a non-dealloced object to work with.

This can be useful in a situation like this (client code):

NSString*thing =[obj title];[obj setTitle:nil];// here you could hit retainCount 0!NSLog(@"Length %d",[thing length]);// here thing might be dealloced already!

The retain (and use of autorelease instead of release) in your title method prevents this code from blowing up. The autoreleased object will not have its release method called until AFTER the current call stack is done executing (end of the current run loop). This gives all client code in the call stack a chance to use this object without worrying about it getting dealloc'ed.

The Important Thing To Remember: This ain't Java, Ruby or PHP. Just because you have a reference to an object in yer [sic] variable does NOT ensure that you won't get it dealloc'ed from under you. You have to retain it, but then you'd have to remember to release it. Autorelease lets you avoid this. You should always use autorelease unless you're dealing with properties or loops with many iterations (and probably not even then unless a problem occurs).

 

转载于:https://www.cnblogs.com/zhangjie/archive/2013/06/10/3131367.html

你可能感兴趣的文章
分段和分页内存管理
查看>>
人工智能教程
查看>>
Redis客户端连接异常
查看>>
小白学数据分析----->移动游戏的使用时长分析
查看>>
静态页面表单提交
查看>>
走进 Spring IOC 的世界
查看>>
【HTML/XML 7】CSS层叠样式表
查看>>
Segmentation fault(Core Dump)
查看>>
SQL Server全文索引 (简单的搜索引擎)
查看>>
关于logstash-out-mongodb插件说明
查看>>
第三届蓝桥杯本科预赛 c++ 第十题
查看>>
redis详解
查看>>
修改列名以及其数据类型
查看>>
实用性较强的idea插件
查看>>
鱼塘钓鱼(fishing)
查看>>
java基础篇---正则表达式
查看>>
声明提升
查看>>
关于c# .net爬虫
查看>>
Rails--render partial时传递参数
查看>>
java基础不牢固容易踩的坑
查看>>