Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Avoid temporary `CowData` `nullptr` state during pointer assignment.

Lukas Tenbrinkf68c3e82f550
diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h
index ccb007a13ac..96211244000 100644
--- a/core/templates/cowdata.h
+++ b/core/templates/cowdata.h
@@ -558,14 +558,16 @@ void CowData<T>::_ref(const CowData &p_from) {
return; // self assign, do nothing.
}
- _unref(); // Resets _ptr to nullptr.
+ // Keep _ptr valid at all times. old_data will unref as it goes out of scope.
+ // This isn't strictly necessary but makes us a bit more resilient to multithread misuse.
+ CowData old_data;
+ old_data._ptr = _ptr;
- if (!p_from._ptr) {
- return; //nothing to do
- }
-
- if (p_from._get_refcount()->conditional_increment() > 0) { // could reference
+ if (p_from._ptr && p_from._get_refcount()->conditional_increment() > 0) {
_ptr = p_from._ptr;
+ } else {
+ // New data is null or we cannot copy.
+ _ptr = nullptr;
}
}