Browse Source

* Fixed get_val2 not using the stack, which automatically frees the data, causing memory leaks for string variables since r11976. (bugreport:723 , part of bugreport:714 and part of bugreport:708)

git-svn-id: https://svn.code.sf.net/p/rathena/svn/trunk@11997 54d463be-8e91-2dee-dedb-b68131a5f0ec
FlavioJS 17 years ago
parent
commit
b465fb5f30
2 changed files with 20 additions and 8 deletions
  1. 3 0
      Changelog-Trunk.txt
  2. 17 8
      src/map/script.c

+ 3 - 0
Changelog-Trunk.txt

@@ -4,6 +4,9 @@ AS OF SVN REV. 5091, WE ARE NOW USING TRUNK.  ALL UNTESTED BUGFIXES/FEATURES GO
 IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK.
 IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK.
 
 
 2007/12/30
 2007/12/30
+	* Fixed get_val2 not using the stack, which automatically frees the data, 
+	  causing memory leaks for string variables since r11976.
+	  (bugreport:723 , part of bugreport:714 and part of bugreport:708)
 	* Trully fixed the previous commits. (missing cast and incomplete size)
 	* Trully fixed the previous commits. (missing cast and incomplete size)
 	* Made the memory manager set allocated memory to 0xCD and freed memory 
 	* Made the memory manager set allocated memory to 0xCD and freed memory 
 	  to 0xDD. The memory manager no longer 'hides' uses of freed memory.
 	  to 0xDD. The memory manager no longer 'hides' uses of freed memory.

+ 17 - 8
src/map/script.c

@@ -2233,15 +2233,17 @@ void get_val(struct script_state* st, struct script_data* data)
 	return;
 	return;
 }
 }
 
 
+void push_val2(struct script_stack* stack, int type, int val, struct linkdb_node** ref);
+
 /// Retrieves the value of a reference identified by uid (variable, constant, param)
 /// Retrieves the value of a reference identified by uid (variable, constant, param)
+/// The value is left in the top of the stack and needs to be removed manually.
 void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
 void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
 {
 {
-	struct script_data data;
-	data.type = C_NAME;
-	data.u.num = uid;
-	data.ref = ref;
-	get_val(st, &data);
-	return (data.type == C_INT ? (void*)data.u.num : (void*)data.u.str);
+	struct script_data* data;
+	push_val2(st->stack, C_NAME, uid, ref);
+	data = script_getdatatop(st, -1);
+	get_val(st, data);
+	return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
 }
 }
 
 
 /*==========================================
 /*==========================================
@@ -4735,6 +4737,7 @@ static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isst
 			char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
 			char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
 			if( str && *str )
 			if( str && *str )
 				ret = idx + 1;
 				ret = idx + 1;
+			script_removetop(st, -1, 0);
 		}
 		}
 	}
 	}
 	else
 	else
@@ -4744,6 +4747,7 @@ static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isst
 			int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
 			int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
 			if( num )
 			if( num )
 				ret = idx + 1;
 				ret = idx + 1;
+			script_removetop(st, -1, 0);
 		}
 		}
 	}
 	}
 	return ret;
 	return ret;
@@ -4941,6 +4945,7 @@ BUILDIN_FUNC(copyarray)
 		{
 		{
 			v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
 			v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
 			set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
 			set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
+			script_removetop(st, -1, 0);
 		}
 		}
 	}
 	}
 	else
 	else
@@ -4948,10 +4953,13 @@ BUILDIN_FUNC(copyarray)
 		for( i = 0; i < count; ++i )
 		for( i = 0; i < count; ++i )
 		{
 		{
 			if( idx2 + i < 128 )
 			if( idx2 + i < 128 )
+			{
 				v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
 				v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
+				set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
+				script_removetop(st, -1, 0);
+			}
 			else// out of range - assume ""/0
 			else// out of range - assume ""/0
-				v = (void*)(is_string_variable(name1) ? "" : 0);
-			set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
+				set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
 		}
 		}
 	}
 	}
 	return 0;
 	return 0;
@@ -5049,6 +5057,7 @@ BUILDIN_FUNC(deletearray)
 		{
 		{
 			void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
 			void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
 			set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
 			set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
+			script_removetop(st, -1, 0);
 		}
 		}
 	}
 	}