Weak References
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Automatic_Reference_Counting_in_Delphi_Mobile_Compilers#Weak_References
Weak References
Another important concept for ARC is the role of weak references, which you can create by tagging them with [weak] attribute. Suppose that two objects refer to each other using a field, and an external variable refers to the first. The reference count of the first object will be 2 (the external variable, and the second object), while the reference count of the second object is 1. Now, as the external variable goes out of scope, the two objects' reference count remains 1, and they remain in memory indefinitely.
To solve this type of situation, and many similar scenarios, is to use a weak reference to break the circular references when the last external reference goes out of scope.
A weak reference is a reference to an object that does not increase its reference count. To declare a weak reference, use the [weak] attribute, supported by the Delphi mobile compilers.
Given the previous scenario, if the reference from the second object back to the first one is weak, as the external variable goes out of scope, both objects are destroyed. Here is an example of this situation:
type
TMyComplexClass = class;
TMySimpleClass = class
private
[Weak] FOwnedBy: TMyComplexClass;
public
constructor Create();
destructor Destroy (); override;
procedure DoSomething(bRaise: Boolean = False);
end;
TMyComplexClass = class
private
fSimple: TMySimpleClass;
public
constructor Create();
destructor Destroy (); override;
class procedure CreateOnly;
end;
In the following code snippet, the constructor of the complex class creates an object of the other class:
constructor TMyComplexClass.Create;
begin
inherited Create;
FSimple := TMySimpleClass.Create;
FSimple.FOwnedBy := self;
end;
Remember that the FOwnedBy field is a weak reference, so it does not increase the reference count of the object it refers to, in this case the current object (self).
Given this class structure, we can write:
class procedure TMyComplexClass.CreateOnly;
var
MyComplex: TMyComplexClass;
begin
MyComplex := TMyComplexClass.Create;
MyComplex.fSimple.DoSomething;
end;
This causes no memory leak, given that the weak reference is properly used.
As a further example of the use of weak references, note this code snippet in the Delphi RTL, part of the TComponent class declaration:
type
TComponent = class(TPersistent, IInterface,
IInterfaceComponentReference)
private
[Weak] FOwner: TComponent;
If you use the weak attribute in code compiled by one of the desktop Delphi compilers, the attribute is ignored. Using DCC32, you have to make sure that you add the proper code in the destructor of an "owner" object to also free the "owned" object. Calling Free is allowed, although the effect is different in the Delphi mobile compilers. The behavior is correct in both in most circumstances.
Note: When an instance has its memory released, all active [weak] references are set to nil. Just as a strong (normal) reference, a [weak] variable can only be nil or reference a valid instance. This is the main reason why a weak reference should be assigned to a strong reference before being tested for nil and dereferenced. Assigning to a strong reference does not allow the instance to be released prematurely.
var
O: TComponent;// a strong reference
begin
O := FOwner; // assigning a weak reference to a strong reference
if O <> nil then
O.<method>;// safe to dereference
end;
Note: You can only pass a [Weak] variable to a var or out parameter that is also marked as [Weak]. You cannot pass a regular strong reference to a [Weak] var or out parameter.
Weak References的更多相关文章
- [翻译]Understanding Weak References(理解弱引用)
原文 Understanding Weak References Posted by enicholas on May 4, 2006 at 5:06 PM PDT 译文 我面试的这几个人怎么这么渣啊 ...
- 【翻译】C# Tips & Tricks: Weak References - When and How to Use Them
原文:C# Tips & Tricks: Weak References - When and How to Use Them Sometimes you have an object whi ...
- Understanding Weak References
Understanding Weak References Posted by enicholas on May 4, 2006 at 5:06 PM PDT Some time ago I was ...
- [转]Understanding Weak References
原文地址:https://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html 推荐另一篇文章:http://www ...
- lua weak table 概念解析
lua weak table 经常看到lua表中有 weak table的用法, 例如: weak_table = setmetatable({}, {__mode="v"}) 官 ...
- strong & weak
Here is a quick summary: A strong reference will keep the object it points to from being deallocated ...
- Lua中的weak表——weak table
弱表(weak table)是一个很有意思的东西,像C++/Java等语言是没有的.弱表的定义是:A weak table is a table whose elements are weak ref ...
- Lua中的weak表——weak table(转)
弱表(weak table)是一个很有意思的东西,像C++/Java等语言是没有的.弱表的定义是:A weak table is a table whose elements are weak ref ...
- OC对象之旅 weak弱引用实现分析
Runtime学习 -- weak应用源码学习 Runtime源码分析,带你了解OC实现过程.其中参考了大量的大神的代码以及文献,里面也有个人的见解,欢迎拍砖,欢迎交流. 两种常见使用场景 /// ...
随机推荐
- [转载]Unicode中对中文字符的编码
以前写过一篇贴子是写中文在unicode中的编码范围 unicode中文范围,但写的不是很详细,今天再次研究了下unicode,并给出详细的unicode取值范围. 本次研究的unicode对象是un ...
- NET MVC 升级到5.1后,View视图中代码报错
使用nuget将项目中MVC4 升级到MVC5,之后项目还可以正常编译运行, 但View视图中相关的很多代码都报错,比如: 1.@model找不到 2.@Html找不到,本该是System.Web.M ...
- iOS8扩展插件开发配置 [转载]
一.iOS8扩展插件概述 WWDC14除了发布了OS X v10.10和switf外,iOS8.0也开始变得更加开放了.说到开放,当然要数应用扩展(App Extension)了.顾名思义,应用扩展允 ...
- log4j 格式详解
### 设置### log4j.rootLogger = debug,stdout,D,E ### 输出信息到控制抬 ### log4j.appender.stdout = org.apache.lo ...
- LOJ2359. 「NOIP2016」天天爱跑步【树上差分】
LINK 思路 首先发现如果对于一个节点,假设一个节点需要统计从字数内来的贡献 需要满足\(dep_u - dep_s = w_u\) 这个条件其实可以转化成\(dep_u - w_u = dep_s ...
- 零基础学习hadoop到上手工作线路指导初级篇:hive及mapreduce
此篇是在零基础学习hadoop到上手工作线路指导(初级篇)的基础,一个继续总结.五一假期:在写点内容,也算是总结.上面我们会了基本的编程,我们需要对hadoop有一个更深的理解:hadoop分为h ...
- Java 并发:volatile 关键字解析
摘要: 在 Java 并发编程中,要想使并发程序能够正确地执行,必须要保证三条原则,即:原子性.可见性和有序性.只要有一条原则没有被保证,就有可能会导致程序运行不正确.volatile关键字 被用来保 ...
- PHP 数组转json_encode,单个数组下标为了0时不对??
在 php 数组转json时,假如 有一个数组下标是顺序的,他json_encode后会直接变成一个简版二维json, $arr = ['1'=>1,'2'=>2]; echo (json ...
- flask第十六篇——Response【2】
今天来介绍自定义返回对象: 现在我们假定有一个需求:所有的视图函数都要返回json格式的对象我们先看一下Response的源码: 发现只有一行default_mimetype='text/html', ...
- scrapy 的基本命令
scrapy stratproject projectname ##创建一个项目 scrapy genspider myspidername fider ##创建一个spider文件 scrapy ...