Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

RefCounted

继承: Object

派生: AESContext, AStar2D, AStar3D, AStarGrid2D, AudioEffectInstance, AudioSample, AudioSamplePlayback, AudioStreamPlayback, CameraFeed, CharFXTransform, ConfigFile, Crypto, DirAccess, DTLSServer, EditorContextMenuPlugin, EditorDebuggerPlugin, EditorDebuggerSession, EditorExportPlatform, EditorExportPlugin, EditorExportPreset, EditorFeatureProfile, EditorFileSystemImportFormatSupportQuery, EditorInspectorPlugin, EditorResourceConversionPlugin, EditorResourcePreviewGenerator, EditorResourceTooltipPlugin, EditorSceneFormatImporter, EditorScenePostImport, EditorScenePostImportPlugin, EditorScript, EditorTranslationParserPlugin, EncodedObjectAsID, ENetConnection, EngineProfiler, Expression, FileAccess, GLTFObjectModelProperty, HashingContext, HMACContext, HTTPClient, ImageFormatLoader, JavaClass, JavaObject, JavaScriptObject, KinematicCollision2D, KinematicCollision3D, Lightmapper, MeshConvexDecompositionSettings, MeshDataTool, MultiplayerAPI, Mutex, NavigationPathQueryParameters2D, NavigationPathQueryParameters3D, NavigationPathQueryResult2D, NavigationPathQueryResult3D, Node3DGizmo, OggPacketSequencePlayback, OpenXRAPIExtension, PackedDataContainerRef, PacketPeer, PCKPacker, PhysicsPointQueryParameters2D, PhysicsPointQueryParameters3D, PhysicsRayQueryParameters2D, PhysicsRayQueryParameters3D, PhysicsShapeQueryParameters2D, PhysicsShapeQueryParameters3D, PhysicsTestMotionParameters2D, PhysicsTestMotionParameters3D, PhysicsTestMotionResult2D, PhysicsTestMotionResult3D, RandomNumberGenerator, RDAttachmentFormat, RDFramebufferPass, RDPipelineColorBlendState, RDPipelineColorBlendStateAttachment, RDPipelineDepthStencilState, RDPipelineMultisampleState, RDPipelineRasterizationState, RDPipelineSpecializationConstant, RDSamplerState, RDShaderSource, RDTextureFormat, RDTextureView, RDUniform, RDVertexAttribute, RegEx, RegExMatch, RenderSceneBuffers, RenderSceneBuffersConfiguration, Resource, ResourceFormatLoader, ResourceFormatSaver, ResourceImporter, SceneState, SceneTreeTimer, Semaphore, SkinReference, StreamPeer, SurfaceTool, TCPServer, TextLine, TextParagraph, TextServer, Thread, TLSOptions, TranslationDomain, TriangleMesh, Tween, Tweener, UDPServer, UPNP, UPNPDevice, WeakRef, WebRTCPeerConnection, XMLParser, XRInterface, XRPose, XRTracker, ZIPPacker, ZIPReader

引用计数对象的基类。

描述

所有进行引用计数的对象的基类。Resource 和许多其他辅助对象都继承自这个类。

与其他 Object 类型不同,RefCounted 内部存在引用计数器,当且仅当自身不再处于使用状态时就会自动释放。因此 RefCounted 不需要使用 Object.free() 手动释放。

陷入循环引用的 RefCounted 实例不会自动释放。例如某节点持有对实例 A 的引用,而该实例直接或间接持有对 A 的引用,则 A 的引用计数为 2。该节点销毁后 A 悬空,引用计数为 1,造成内存泄漏。为了防止这种情况,可以使用 @GlobalScope.weakref() 将循环中的某一处引用设置为弱引用。

在绝大多数用例中,只需实例化和使用 RefCounted 派生类型即可。该类中提供的方法仅适用于高级用户,如果使用不当可能会导致问题。

注意:在 C# 中,引用计数的对象在不再使用后不会立即释放。相反,垃圾收集将定期运行,并释放不再使用的引用计数对象。这意味着未使用的引用计数对象会在被移除之前在内存中保留一段时间。

教程

方法

int

get_reference_count() const

bool

init_ref()

bool

reference()

bool

unreference()


方法说明

int get_reference_count() const 🔗

返回当前的引用计数。


bool init_ref() 🔗

初始化内部引用计数器。不清楚有什么用就别用。

返回初始化是否成功。


bool reference() 🔗

将内部引用计数器加一。不清楚有什么用就别用。

如果增加成功则返回 true,否则返回 false


bool unreference() 🔗

将内部引用计数器减一。不清楚有什么用就别用。

如果减少成功则返回 true,否则返回 false