Skip to content
0.2.0-dev Download ZIP
45 lines ·2.9 KB Raw
1<?xml version="1.0" encoding="UTF-8" ?>
2<class name="RefCounted" inherits="Object" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 Base class for reference-counted objects.
5 </brief_description>
6 <description>
7 Base class for any object that keeps a reference count. [Resource] and many other helper objects inherit this class.
8 Unlike other [Object] types, [RefCounted]s keep an internal reference counter so that they are automatically released when no longer in use, and only then. [RefCounted]s therefore do not need to be freed manually with [method Object.free].
9 [RefCounted] instances caught in a cyclic reference will [b]not[/b] be freed automatically. For example, if a node holds a reference to instance [code]A[/code], which directly or indirectly holds a reference back to [code]A[/code], [code]A[/code]'s reference count will be 2. Destruction of the node will leave [code]A[/code] dangling with a reference count of 1, and there will be a memory leak. To prevent this, one of the references in the cycle can be made weak with [method @GlobalScope.weakref].
10 In the vast majority of use cases, instantiating and using [RefCounted]-derived types is all you need to do. The methods provided in this class are only for advanced users, and can cause issues if misused.
11 [b]Note:[/b] In C#, reference-counted objects will not be freed instantly after they are no longer in use. Instead, garbage collection will run periodically and will free reference-counted objects that are no longer in use. This means that unused ones will remain in memory for a while before being removed.
12 </description>
13 <tutorials>
14 <link title="When and how to avoid using nodes for everything">$DOCS_URL/tutorials/best_practices/node_alternatives.html</link>
15 </tutorials>
16 <methods>
17 <method name="get_reference_count" qualifiers="const">
18 <return type="int" />
19 <description>
20 Returns the current reference count.
21 </description>
22 </method>
23 <method name="init_ref">
24 <return type="bool" />
25 <description>
26 Initializes the internal reference counter. Use this only if you really know what you are doing.
27 Returns whether the initialization was successful.
28 </description>
29 </method>
30 <method name="reference">
31 <return type="bool" />
32 <description>
33 Increments the internal reference counter. Use this only if you really know what you are doing.
34 Returns [code]true[/code] if the increment was successful, [code]false[/code] otherwise.
35 </description>
36 </method>
37 <method name="unreference">
38 <return type="bool" />
39 <description>
40 Decrements the internal reference counter. Use this only if you really know what you are doing.
41 Returns [code]true[/code] if the object should be freed after the decrement, [code]false[/code] otherwise.
42 </description>
43 </method>
44 </methods>
45</class>