Browse Source

[godot] Closes #2489, fix reference count of SpineAttachments

The SpineAttachment destructor would dereference the spine::Attachment. However, the set_spine_object() call doesn't increase the reference count. This can lead to the attachment being deallocated too early.
Mario Zechner 1 year ago
parent
commit
5abed8a7f6

+ 7 - 0
spine-godot/spine_godot/SpineAttachment.h

@@ -30,6 +30,7 @@
 #pragma once
 
 #include "SpineCommon.h"
+#include "spine/Attachment.h"
 #include <spine/spine.h>
 
 class SpineSkeletonDataResource;
@@ -46,4 +47,10 @@ public:
 	String get_attachment_name();
 
 	Ref<SpineAttachment> copy();
+
+	void set_spine_object(const SpineSkeletonDataResource *_owner, spine::Attachment *_object) {
+		if (get_spine_object()) get_spine_object()->dereference();
+		_set_spine_object_internal(_owner, _object);
+		if (_object) _object->reference();
+	}
 };

+ 1 - 1
spine-godot/spine_godot/SpineCommon.h

@@ -145,7 +145,7 @@ class SpineSkeletonDataResource;
 template<typename OBJECT>
 class SpineSkeletonDataResourceOwnedObject : public SpineObjectWrapper {
 public:
-	void set_spine_object(const SpineSkeletonDataResource *_owner, OBJECT *_object) {
+	virtual void set_spine_object(const SpineSkeletonDataResource *_owner, OBJECT *_object) {
 		_set_spine_object_internal(_owner, _object);
 	}