官术网_书友最值得收藏!

Classes

While classes look superficially similar to records, they are in fact implemented in a completely different manner.

When you create an object of a class by calling the constructor for that class, the code allocates memory from the memory manager and fills it with zeroes. That, in effect, initializes all fields, managed and unmanaged, to default values. A pointer to this memory is stored in the variable which receives the result of a constructor.

When an object is destroyed, its memory is returned to the system (again, through the memory manager).

The rest of this section applies only to standard compilers (Window 32- and 64-bit and OS/X). If you are using an ARC-enabled compiler (Android, iOS, and Linux) then classes behave the same as interfaces. See the next section for more details.

Copying one variable of a class type to another just copies the pointer. In effect, you then have two variables pointing to the same object. There is no reference counting associating with objects and if you now destroy one variable (by calling the Free method), the other will still point to the same part of memory. Actually, both variables will point to that unused memory and if it gets allocated later (and it will), both variables will point to memory belonging to another object:

var
o1, o2: TObject;
begin
o1 := TObject.Create;
o2 := o1;
// o1 and o2 now point to the same object
o1.Free;
// o1 and o2 now point to unowned memory
o1 := nil;
// o2 still points to unowned memory
end;

If you have a long-living variable or field containing an object of some class and you destroy the object, make sure that you also set the value/field to nil

主站蜘蛛池模板: 洛阳市| 多伦县| 建德市| 临邑县| 郎溪县| 花莲县| 航空| 安多县| 海林市| 元朗区| 旬邑县| 柘荣县| 南华县| 安丘市| 视频| 长沙县| 长武县| 桦南县| 靖安县| 天水市| 奎屯市| 舟山市| 武清区| 鹤壁市| 紫阳县| 梅河口市| 亳州市| 金堂县| 花莲市| 焉耆| 延安市| 中西区| 安丘市| 辉南县| 潮州市| 常山县| 遵化市| 长沙市| 阳江市| 商河县| 宁安市|