-
-
Notifications
You must be signed in to change notification settings - Fork 595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: GDnative Reference Counting #281
Comments
After discussing this on discord, I believe to have part of my answers. There are two main ways GDnative objects can hold onto nodes:
There is a method Suggestion: add |
Until recently there have been a number of bugs relating to I've attempted to summarize the current state & suggested approach in #417. |
Request for documentation
I'm having a hard time figuring out the rules for dealing with references to other nodes and types in GDnative.
Documentation on this seems to be sparse and I'm worried about producing crashes (by not incrementing the reference counter when taking ownership of something) or leaking memory (because I'm not releasing references properly).
It would be very useful to provide some documentation, or even just a set of rules, on how to deal with references in GDnative.
The following 3 question cover most of what I feel is unclear right now and having answers to them would help a great deal:
godot::Reference
(refcounted) or fromgodot::Object
(not refcounted)?1a. If the class inherits from
godot::Object
, how is ownership handled?1b. If the class inherits from
godot::Reference
, when do I increment/decrement the reference counter?If a new object starts out with a reference count of 1, then I can't decrement it before returning the object.
Thus, it follows that returning an object must also share ownership:
If I instead returned an object that I was holding (i.e.
this->active_move
), I'd have to increment the reference counter before doing soWhen I call any method that returns an object, I need to decrement the reference counter on the returned object after I'm done with it.
Reference
Does this mean nodes always belong to the scene tree?
If so, wouldn't it be dangerous to store references to nodes because if the node gets destroyed (i.e. enemy dies, etc.), anyone holding a reference to it would have a dangling pointer?
Or do it need to get some 'instance id', 'rid' or somesuch and always look it up via Godot's scene server?
Existing documentation (or rather, attempts to find it :))
Godot Variants do call
.ref.unref()
on objects duringclear()
https://github.com/godotengine/godot/blob/master/core/variant.cpp#L1107-L1111
Godot's
reference.h
contains a helper classRef<T>
which does manipulate reference countershttps://github.com/godotengine/godot/blob/master/core/reference.h#L64
godot-cpp has a
Ref<T>
class with a comment that states it's replicating Godot's ownRef<T>
classhttps://github.com/GodotNativeTools/godot-cpp/blob/master/include/core/Ref.hpp#L10
The text was updated successfully, but these errors were encountered: