Browse Source

Implement copy assignment for AutoKeyValues

This resolves issues with the server crashing; it looks like it was
just copying the pointer itself.

Working with KeyValues is miserable.
nosoop 1 year ago
parent
commit
8a6c4f2865
1 changed files with 13 additions and 4 deletions
  1. 13 4
      econmanager.h

+ 13 - 4
econmanager.h

@@ -44,6 +44,7 @@ public:
 
 /**
  * Copied implementation of KeyValues::AutoDelete.
+ * This implementation creates copies of any KeyValues pointers passed into it.
  */
 class AutoKeyValues {
 	public:
@@ -52,11 +53,19 @@ class AutoKeyValues {
 	AutoKeyValues(KeyValues *pKeyValues) : m_pKeyValues{pKeyValues->MakeCopy()} {}
 	AutoKeyValues(const AutoKeyValues &other) : m_pKeyValues{other.m_pKeyValues->MakeCopy()} {}
 	
+	/**
+	 * Copy assignment.  The contents of the KeyValues* needs to be copied; the default
+	 * implementation seems to cause a use-after-free.
+	 */
+	AutoKeyValues& operator =(const AutoKeyValues &other) {
+		this->Assign(other);
+		return *this;
+	}
+	
 	~AutoKeyValues() {
-		// TODO: figure out why doing this blows up the server
-		// if (m_pKeyValues) {
-			// m_pKeyValues->deleteThis();
-		// }
+		if (m_pKeyValues) {
+			m_pKeyValues->deleteThis();
+		}
 	}
 	
 	void Assign(KeyValues *pKeyValues) {