[code] Question on correctly freeing memory.

From: Gary Barnett (gbarnett@polarnet.com)
Date: 09/13/96


I am working on having customized object descriptions save with the objects in
the rent system. I have the code working correcty, but am not sure of the correct
way to free the memory in use before assigning the new description. Here is
the relevant code.. Any thoughts on how to plug the resulting memory leak will
be most appreciated:

Added to "Obj_from_store_to:"
...
x    GET_OBJ_DAMAGE(obj) = object.damage;
x    GET_OBJ_HARDINESS(obj) = object.hardiness;
+	/* adding the new string replacement stuff */
+	obj->short_description = str_dup(object.short_description);
+	obj->description = str_dup(object.description);
+	obj->action_description = str_dup(object.action_description);
+	obj->name = str_dup(object.name);
x     for (j = 0; j < MAX_OBJ_AFFECT; j++)
x     obj->affected[j] = object.affected[j];

Added to "Obj_to_store_from"

x object.timer = GET_OBJ_TIMER(obj);
x  object.bitvector = obj->obj_flags.bitvector;
x object.damage = GET_OBJ_DAMAGE(obj);
x  object.hardiness = GET_OBJ_HARDINESS(obj);
+  /* adding the new string replacement stuff */
+   strcpy(object.short_description, obj->short_description);
+	strcpy(object.description, obj->description);
+	if (obj->action_description) {
+		strcpy(object.action_description, obj->action_description);
+   } else {
+		strcpy(object.action_description, "<none>");
+	}
+	strcpy(object.name, obj->name);
x  for (j = 0; j < MAX_OBJ_AFFECT; j++)
x   object.affected[j] = obj->affected[j];

Added to (in structs.h)  to "struct obj_file_elem"

x struct obj_affected_type affected[MAX_OBJ_AFFECT];
+   char	name[MAX_INPUT_LENGTH];
+   char description[MAX_INPUT_LENGTH];
+   char	short_description[MAX_INPUT_LENGTH];
+   char	action_description[MAX_STRING_LENGTH];
x };

As you can see, no attempt is made to free memory.. I'm not real proficient with
dynamic memory allocation as yet.. 

btw the hardiness and damage updates are my own creation.. other than those two
ints, and this new code, nothing in the obj renting routines have been modified. 

Thanks,

Gary Barnett
- http://www.polarnet.com/Users/gbarnett/ - telnet:mud.polarnet.com (4000)

+-----------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   http://cspo.queensu.ca/~fletcher/Circle/list_faq.html   |
+-----------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/18/00 PST