Ronald Oussoren avatar Ronald Oussoren committed 7dded5d

- Better fix for addressbook.py: explicitly access the type of the object
instead of hardcoding a class name
- Use PyMem_Malloc et. al. instead of malloc
We were sometimes PyMem_Free-ing malloc-ed data :-(
- Minor fix for GNU runtime: in class-list.m
GNU runtime support is not working at the moment, it is not possible to
define new classes.

Comments (0)

Files changed (12)

pyobjc/Examples/addressbook.py

 """
     sys.exit(1)
 
-propNames = AddressBook.ABPerson.properties()
+# Ask the class which properties are supported.
+propNames = type(me).properties()
+
 d = {}
 for i in propNames:
     d[i] = me.valueForProperty_(i)

pyobjc/Modules/objc/class-builder.m

 
 	PyObjCRT_ClearClass(&(wrapper->class));
 	PyObjCRT_ClearClass(&(wrapper->meta_class));
-	free(objc_class);
+	PyMem_Free(objc_class);
 }
 
 /*
 	}
 
 	/* Allocate the class as soon as possible, for new selector objects */
-	new_class = calloc(1, sizeof(struct class_wrapper));
+	new_class = PyMem_Malloc(sizeof(struct class_wrapper));
 	if (new_class == NULL) {
 		goto error_cleanup;
 	}
+	memset(new_class, 0, sizeof(*new_class));
 
 	/* First round, count new instance-vars and check for overridden 
 	 * methods.
 	if (ivar_count == 0)  {
 		ivar_list = NULL;
 	} else {
-		ivar_list = malloc(sizeof(struct objc_ivar_list) +
+		ivar_list = PyMem_Malloc(sizeof(struct objc_ivar_list) +
 			(ivar_count)*sizeof(struct objc_ivar));
 		if (ivar_list == NULL) {
 			PyErr_NoMemory();
 	}
 
 	if (ivar_list) {
-		free(ivar_list);
+		PyMem_Free(ivar_list);
 	}
 	if (method_list) {
-		free(method_list);
+		PyMem_Free(method_list);
 	}
 	if (meta_method_list) {
-		free(meta_method_list);
+		PyMem_Free(meta_method_list);
 	}
 
 	if (new_class != NULL) {
 		PyObjCRT_ClearClass(&(new_class->class));
 		PyObjCRT_ClearClass(&(new_class->meta_class));
-		free(new_class);
+		PyMem_Free(new_class);
 	}
 
 	return NULL;

pyobjc/Modules/objc/class-list.m

 			Class*    newBuffer;
 			bufferLen = neededLen;
 
-			/* realloc(NULL, ...) might not work, call malloc when
+			/* Realloc(NULL, ...) might not work, call Malloc when
 			 * the buffer is NULL.
 			 */
 			if (buffer == NULL) {
-				newBuffer = malloc(sizeof(Class) * bufferLen);
+				newBuffer = PyMem_Malloc(sizeof(Class) * bufferLen);
 			} else {
-				newBuffer = realloc(buffer, 
+				newBuffer = PyMem_Realloc(buffer, 
 						sizeof(Class) * bufferLen);
 			}
 			if (newBuffer == NULL) {
 	}
 
 	if (buffer != initialBuffer) {
-		free(buffer); buffer = NULL;
+		PyMem_Free(buffer); buffer = NULL;
 	}
 	return result;
 
 error:
 	if (buffer && buffer != initialBuffer) {
-		free(buffer);
+		PyMem_Free(buffer);
 		buffer = NULL;
 	}
 	Py_XDECREF(result);
 
 	while ((classid = objc_next_class(&state))) len++;
 
-	result = PyTuple_New(i);
+	result = PyTuple_New(len);
 
 	state = NULL; i = 0;
 

pyobjc/Modules/objc/libffi_support.m

 static void
 free_type(void *obj)
 {
-	free(((ffi_type*)obj)->elements);
-	free(obj);
+	PyMem_Free(((ffi_type*)obj)->elements);
+	PyMem_Free(obj);
 }
 
 static ffi_type* signature_to_ffi_type(const char* argtype);
 	 */
 	field_count = atoi(argtype+1);
 			
-	type = malloc(sizeof(*type));
+	type = PyMem_Malloc(sizeof(*type));
 	if (type == NULL) {
 		PyErr_NoMemory();
 		return NULL;
 	 * fine on MacOS X.
 	 */
 	type->type = FFI_TYPE_STRUCT;
-	type->elements = malloc((1+field_count) * sizeof(ffi_type*));
+	type->elements = PyMem_Malloc((1+field_count) * sizeof(ffi_type*));
 	if (type->elements == NULL) {
-		free(type);
+		PyMem_Free(type);
 		PyErr_NoMemory();
 		return NULL;
 	}
 		return NULL;
 	}
 			
-	type = malloc(sizeof(*type));
+	type = PyMem_Malloc(sizeof(*type));
 	if (type == NULL) {
 		PyErr_NoMemory();
 		return NULL;
 	type->size = PyObjCRT_SizeOfType(argtype);
 	type->alignment = PyObjCRT_AlignOfType(argtype);
 	type->type = FFI_TYPE_STRUCT;
-	type->elements = malloc((1+field_count) * sizeof(ffi_type*));
+	type->elements = PyMem_Malloc((1+field_count) * sizeof(ffi_type*));
 	if (type->elements == NULL) {
-		free(type);
+		PyMem_Free(type);
 		PyErr_NoMemory();
 		return NULL;
 	}
 			type->elements[field_count] = 
 				signature_to_ffi_type(curtype);
 			if (type->elements[field_count] == NULL) {
-				free(type->elements);
+				PyMem_Free(type->elements);
 				return NULL;
 			}
 			field_count++;
 			curtype = PyObjCRT_SkipTypeSpec(curtype);
 			if (curtype == NULL) {
-				free(type->elements);
+				PyMem_Free(type->elements);
 				return NULL;
 			}
 		}
 		return NULL;
 	}
 
-	stubUserdata = malloc(sizeof(*stubUserdata));
+	stubUserdata = PyMem_Malloc(sizeof(*stubUserdata));
 	if (stubUserdata == NULL) {
 		PyObjCMethodSignature_Free(methinfo);
 		return NULL;
 		if (stubUserdata->callable) {
 			Py_DECREF(stubUserdata->callable);
 		}
-		free(stubUserdata);
+		PyMem_Free(stubUserdata);
 		return NULL;
 	}
 
 	}
 
 	/* Build FFI argumentlist description */
-	cl_arg_types = malloc(sizeof(ffi_type*) * (argOffset+methinfo->nargs));
+	cl_arg_types = PyMem_Malloc(sizeof(ffi_type*) * (argOffset+methinfo->nargs));
 	if (cl_arg_types == NULL) {
-		free(cl_ret_type);
+		PyMem_Free(cl_ret_type);
 		PyErr_NoMemory();
 		return NULL;
 	}
 		cl_arg_types[i+argOffset] = arg_signature_to_ffi_type(
 			methinfo->argtype[i]);
 		if (cl_arg_types[i+argOffset] == NULL) {
-			free(cl_arg_types);
+			PyMem_Free(cl_arg_types);
 			return NULL;
 		}
 	}
 
 	/* Create the invocation description */
-	cif = malloc(sizeof(*cif));
+	cif = PyMem_Malloc(sizeof(*cif));
 	if (cif == NULL) {
-		free(cl_arg_types);
+		PyMem_Free(cl_arg_types);
 		PyErr_NoMemory();
 		return NULL;
 	}
 	rv = ffi_prep_cif(cif, FFI_DEFAULT_ABI, methinfo->nargs+argOffset, 
 		cl_ret_type, cl_arg_types);
 	if (rv != FFI_OK) {
-		free(cl_arg_types);
+		PyMem_Free(cl_arg_types);
 		PyErr_Format(PyExc_RuntimeError,
 			"Cannot create FFI CIF: %d", rv);
 		return NULL;
 void
 PyObjCFFI_FreeCIF(ffi_cif* cif)
 {
-	if (cif->arg_types) free(cif->arg_types);
-	free(cif);
+	if (cif->arg_types) PyMem_Free(cif->arg_types);
+	PyMem_Free(cif);
 }
 
 /*
 	}
 
 	/* And finally create the actual closure */
-	cl = malloc(sizeof(*cl));
+	cl = PyMem_Malloc(sizeof(*cl));
 	if (cl == NULL) {
 		PyObjCFFI_FreeCIF(cif);
 		PyErr_NoMemory();
 	cl = (ffi_closure*)closure;
 	retval = cl->user_data;
 	PyObjCFFI_FreeCIF(cl->cif);
-	free(cl);
+	PyMem_Free(cl);
 
 	return retval;
 }

pyobjc/Modules/objc/method-signature.m

 		nargs++;
 	}
 
-	retval = malloc(sizeof(PyObjCMethodSignature) + sizeof(char*)*nargs);
+	retval = PyMem_Malloc(sizeof(PyObjCMethodSignature) + sizeof(char*)*nargs);
 	if (retval == NULL) {
 		PyErr_NoMemory();
 		return NULL;
 	retval->retainCount = 1;
 	retval->signature = PyObjCUtil_Strdup(signature);
 	if (retval->signature == NULL) {
-		free(retval);
+		PyMem_Free(retval);
 		return NULL;
 	}
 
 {
 	if (--value->retainCount != 0) return;
 
-	free((char*)(value->signature));
-	free(value);
+	PyMem_Free((char*)(value->signature));
+	PyMem_Free(value);
 }
 
 

pyobjc/Modules/objc/objc-class.m

 				PyObjCSelector_Signature(newVal));
 
 		if (objcMethod->method_types == NULL) {
-			free(methodsToAdd);
+			PyMem_Free(methodsToAdd);
 			Py_DECREF(newVal);
 			return -1;
 		}
 		objcMethod->method_imp = ObjC_MakeIMPForPyObjCSelector(
 				(PyObjCSelector*)newVal);
 		if (objcMethod->method_imp == NULL) {
-			free((char*)objcMethod->method_types);
-			free(methodsToAdd);
+			PyMem_Free((char*)objcMethod->method_types);
+			PyMem_Free(methodsToAdd);
 			Py_DECREF(newVal);
 			PyErr_NoMemory();
 			return -1;
 		r = PyDict_SetItem(((PyTypeObject*)self)->tp_dict, name, newVal);
 		Py_DECREF(newVal);
 		if (r == -1) {
-			free((char*)objcMethod->method_types);
-			free(methodsToAdd);
+			PyMem_Free((char*)objcMethod->method_types);
+			PyMem_Free(methodsToAdd);
 			PyErr_NoMemory();
 			return -1;
 		}

pyobjc/Modules/objc/objc-runtime-apple.m

 	}
 	metaCls->name = PyObjCUtil_Strdup(name);
 	if (metaCls->name == NULL) {
-		free((char*)(cls->name));
+		PyMem_Free((char*)(cls->name));
 		return -1;
 	}
 
-	cls->methodLists = calloc(1, sizeof(struct objc_method_list*));
+	cls->methodLists = PyMem_Malloc(sizeof(struct objc_method_list*));
 	if (cls->methodLists == NULL) {
 		PyErr_NoMemory();
-		free((char*)(cls->name)); 
-		free((char*)(metaCls->name)); 
+		PyMem_Free((char*)(cls->name)); 
+		PyMem_Free((char*)(metaCls->name)); 
 		return -1;
 	}
+	memset(cls->methodLists, 0, sizeof(*(cls->methodLists)));
 
-	metaCls->methodLists = calloc(1, sizeof(struct objc_method_list*));
+	metaCls->methodLists = PyMem_Malloc(sizeof(struct objc_method_list*));
 	if (cls->methodLists == NULL) {
 		PyErr_NoMemory();
-		free((char*)(cls->name)); 
-		free((char*)(metaCls->name)); 
-		free(cls->methodLists);
+		PyMem_Free((char*)(cls->name)); 
+		PyMem_Free((char*)(metaCls->name)); 
+		PyMem_Free(cls->methodLists);
 		return -1;
 	}
+	memset(metaCls->methodLists, 0, sizeof(*(metaCls->methodLists)));
 
         /*
          * This is MacOS X specific, and an undocumented feature (long live
 			cur = cls->methodLists;
 			while (*cur != (struct objc_method_list*)-1) {
 				if (*cur != NULL) {
-					free(*cur);
+					PyMem_Free(*cur);
 				}
 				cur++;
 			}
-			free(cls->methodLists);
+			PyMem_Free(cls->methodLists);
 			cls->methodLists = NULL;
 		}
 		cls->methodLists = NULL;
 	}
 
 	if (cls->name) {
-		free((char*)(cls->name));
+		PyMem_Free((char*)(cls->name));
 	}
 }
 
 {
         struct objc_method_list *mlist;
 
-        mlist = malloc(sizeof(struct objc_method_list)
+        mlist = PyMem_Malloc(sizeof(struct objc_method_list)
                  + ((numMethods+1) * sizeof(struct objc_method)));
 
         if (mlist == NULL) {

pyobjc/Modules/objc/objc-runtime-gnu.m

 	}
 	metaCls->name = PyObjCUtil_Strdup(name);
 	if (metaCls->name == NULL) {
-		free((char*)(cls->name));
+		PyMem_Free((char*)(cls->name));
 		return -1;
 	}
 
 		while (cur != NULL) {
 			next = cur->method_next;
 
-			free(cur);
+			PyMem_Free(cur);
 			cur = next;
 		}
 		cls->methods = NULL;
 	}
 
 	if (cls->name) {
-		free((char*)(cls->name));
+		PyMem_Free((char*)(cls->name));
 	}
 }
 
 {
         struct objc_method_list *mlist;
 
-        mlist = malloc(sizeof(struct objc_method_list)
+        mlist = PyMem_Malloc(sizeof(struct objc_method_list)
                  + ((numMethods+1) * sizeof(struct objc_method)));
 
         if (mlist == NULL) {

pyobjc/Modules/objc/objc_util.m

 	char* result;
 
 	len = strlen(value);
-	result = malloc(len+1);
+	result = PyMem_Malloc(len+1);
 	if (result == NULL) return NULL;
 
 	memcpy(result, value, len);
 void    PyObjC_FreeCArray(int code, void* array)
 {
 	if (code == SHOULD_FREE) {
-		free(array);
+		PyMem_Free(array);
 	}
 }
 
 					"least %d", seqlen, pycount);
 			return -1;
 		}
-		*array = malloc(eltsize * pycount);
+		*array = PyMem_Malloc(eltsize * pycount);
 		if (*array == NULL) {
 			Py_DECREF(seq);
 			PyErr_NoMemory();
 					((char*)*array)+(i*eltsize));
 			if (r == -1) {
 				Py_DECREF(seq);
-				free(*array); *array = NULL;
+				PyMem_Free(*array); *array = NULL;
 				return -1;
 			}
 		}

pyobjc/Modules/objc/pointer-support.m

 	struct wrapper* value;
 
 	if (items == NULL) {
-		items = malloc(sizeof(struct wrapper));
+		items = PyMem_Malloc(sizeof(struct wrapper));
 		if (items == NULL) {
 			PyErr_NoMemory();
 			return -1;
 	} else {
 		struct wrapper* tmp;
 
-		tmp = realloc(items, sizeof(struct wrapper) *  (item_count+1));
+		tmp = PyMem_Realloc(items, sizeof(struct wrapper) *  (item_count+1));
 		if (tmp == NULL) {
 			PyErr_NoMemory();
 			return -1;

pyobjc/Modules/objc/selector.m

 	value->selector = selector;
 	value->signature = PyObjCUtil_Strdup(signature);
 	if (value->signature == NULL) {
-		free(value->class_name);
+		PyMem_Free(value->class_name);
 		PyErr_NoMemory();
 		return -1;
 	}
 		PyObjCMethodSignature_Retain(result->sel_oc_signature);
 	}
 
-	result->sel_self       = obj;
+	result->sel_self = obj;
 	if (result->sel_self) {
 		Py_INCREF(result->sel_self);
 	}

pyobjc/Modules/objc/wrapper-const-table.h

 	size_t i;
 	int retVal = 0;
 
-	ptrs = malloc(sizeof(void*) * count);
+	ptrs = PyMem_Malloc(sizeof(void*) * count);
 	if (ptrs == NULL) {
 		PyErr_NoMemory();
 		return -1;
 
 cleanup:
 	if (ptrs) {
-		free(ptrs);
+		PyMem_Free(ptrs);
 	}
 
 	[names release];
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.