Browse Source

added class member, safety check in the destructor

DhruvMaroo 4 years ago
parent
commit
cd05371ce8
2 changed files with 9 additions and 5 deletions
  1. 1 0
      include/core/String.hpp
  2. 8 5
      src/core/String.cpp

+ 1 - 0
include/core/String.hpp

@@ -17,6 +17,7 @@ class CharString {
 	friend class String;
 	friend class String;
 
 
 	godot_char_string _char_string;
 	godot_char_string _char_string;
+	bool _deleted = false;
 
 
 public:
 public:
 	~CharString();
 	~CharString();

+ 8 - 5
src/core/String.cpp

@@ -74,12 +74,15 @@ String::String(const String &other) {
 }
 }
 
 
 String::String(String&& other) {
 String::String(String&& other) {
-	godot::api->godot_string_new_copy(&_godot_string, &other._godot_string);
-	godot::api->godot_string_destroy(&_godot_string);
+	_godot_string = other._godot_string;
+	other._deleted = true;
 }
 }
 
 
 String::~String() {
 String::~String() {
-	godot::api->godot_string_destroy(&_godot_string);
+	if (!_deleted) {
+		godot::api->godot_string_destroy(&_godot_string);
+		_deleted = true;
+	}
 }
 }
 
 
 wchar_t &String::operator[](const int idx) {
 wchar_t &String::operator[](const int idx) {
@@ -101,8 +104,8 @@ void String::operator=(const String &s) {
 
 
 void String::operator=(String&& s) {
 void String::operator=(String&& s) {
 	godot::api->godot_string_destroy(&_godot_string);
 	godot::api->godot_string_destroy(&_godot_string);
-	godot::api->godot_string_new_copy(&_godot_string, &s._godot_string);
-	godot::api->godot_string_destroy(&s._godot_string);
+	_godot_string = s._godot_string;
+	s._deleted = true;
 }
 }
 
 
 bool String::operator==(const String &s) const {
 bool String::operator==(const String &s) const {