Commits

Ronald Oussoren committed fd1b7c1

Fix memory leak in py3k port

Comments (0)

Files changed (7)

pyobjc-core/Modules/objc/OC_PythonObject.h

 + depythonifyTable;
 + pythonifyStructTable;
 + (PyObject *)__pythonifyStruct:(PyObject *) obj withType:(const char *) type length:(Py_ssize_t) length;
-+ objectWithCoercedObject:(PyObject *) obj;
-- initWithObject:(PyObject *) obj;
++ objectWithCoercedPyObject:(PyObject *) obj;
+- initWithPyObject:(PyObject *) obj;
 
 /*!
  * @method pyObject

pyobjc-core/Modules/objc/OC_PythonObject.m

 	} else {
 		PyObjC_DURING
 			rval = [OC_PythonObject 
-				objectWithCoercedObject:argument];
+				objectWithCoercedPyObject:argument];
 
 			r = 0;
 
 	if (likely(PyObjCObject_Check(obj))) {
 		instance = PyObjCObject_GetObject(obj);
 	} else {
-		instance = [[self alloc] initWithObject:obj];
+		instance = [[self alloc] initWithPyObject:obj];
 		[instance autorelease];
 	}
 	return instance;
 }
 
-+ objectWithCoercedObject:(PyObject *)obj
++ objectWithCoercedPyObject:(PyObject *)obj
 {
 	id instance;
 	PyObjC_BEGIN_WITH_GIL
 		}
 
 		/* If all else fails use the generic proxy */
-		instance = [[self alloc] initWithObject:obj];
+		instance = [[self alloc] initWithPyObject:obj];
 	PyObjC_END_WITH_GIL
 	[instance autorelease];
 	return instance;
 	return PyObject_CallFunctionObjArgs(convert, obj, NULL);
 }
 
-- initWithObject:(PyObject *) obj
+- initWithPyObject:(PyObject *) obj
 {
 	PyObjC_BEGIN_WITH_GIL
 		if (pyObject) {

pyobjc-core/Modules/objc/class-builder.m

 		PyObject* clsDict; 
 		PyObject* clsValues;
 		PyObject* o;
-		volatile Py_ssize_t i;
-		Py_ssize_t len;
 
-		if (objcClass == nil) break;
+		if (objcClass == nil) {
+			break;
+		}
 
 
 		clsDict = PyObject_GetAttrString(cls, "__dict__");
 		
 		/* Class.__dict__ is a dictproxy, which is not a dict and
 		 * therefore PyDict_Values doesn't work.
+		 *
+		 * XXX: PyMapping_Values?
 		 */
 		clsValues = PyObject_CallMethod(clsDict, "values", NULL);
 		Py_DECREF(clsDict);
 			break;
 		}
 
-		len = PyList_Size(clsValues);
+		PyObject* iter = PyObject_GetIter(clsValues);
+		Py_DECREF(clsValues);
+		if (iter == NULL) {
+			PyErr_Clear();
+			continue;
+		}
+
+
+
 		/* Check type */
-		for (i = 0; i < len; i++) {
+		while ((o = PyIter_Next(iter)) != NULL) {
 			PyObjCInstanceVariable* iv;
 
-			o = PyList_GET_ITEM(clsValues, i);
-
-			if (o == NULL) continue;
-			if (!PyObjCInstanceVariable_Check(o)) continue;
+			if (!PyObjCInstanceVariable_Check(o)) {
+				Py_DECREF(o);
+				continue;
+			}
 		
 			iv = ((PyObjCInstanceVariable*)o);
 
-			if (iv->isOutlet) continue;
-			if (strcmp(iv->type, "@") != 0 && strcmp(iv->type, @encode(PyObject*)) != 0) continue;
+			if (iv->isOutlet) {
+				Py_DECREF(o);
+				continue;
+			}
+			if (strcmp(iv->type, "@") != 0 && strcmp(iv->type, @encode(PyObject*)) != 0) {
+				Py_DECREF(o);
+				continue;
+			}
 
 			var = class_getInstanceVariable(objcClass, iv->name);
-			if (var == NULL) continue;
+			if (var == NULL) {
+				Py_DECREF(o);
+				continue;
+			}
 
 			if (iv->isSlot) {
 				Py_XDECREF(*(PyObject**)(((char*)self) + 
 				PyObjC_ENDHANDLER
 				*(id*)(((char*)self) + ivar_getOffset(var)) = NULL;
 			}
+			Py_DECREF(o);
 		}
-
-		Py_DECREF(clsValues);
+		Py_DECREF(iter);
 
 		o = PyObject_GetAttrString(cls, "__bases__");
 		if (o == NULL) {

pyobjc-core/Modules/objc/formal-protocol.m

 	for (i = 0; i < len; i++) {
 		PyObject* sel = PySequence_Fast_GET_ITEM(selectors, i);
 		SEL theSel = PyObjCSelector_GetSelector(sel);
-		char* theSignature = PyObjCSelector_Signature(sel);
+		const char* theSignature = PyObjCSelector_Signature(sel);
 
 		if (PyObjCSelector_GetFlags(sel)&PyObjCSelector_kCLASS_METHOD) {
 			c = &(theProtocol->class_methods->list[

pyobjc-core/Modules/objc/libffi_support.m

 					return -1;
 				}
 
-				if (PyUnicode_AsWideChar((PyUnicodeObject*)v, (wchar_t*)byref[curarg], sz)<0) {
+				if (PyUnicode_AsWideChar(v, (wchar_t*)byref[curarg], sz)<0) {
 					Py_DECREF(encoded);
 					return -1;
 				}

pyobjc-core/Modules/objc/objc_support.m

 		/* We're depythonifying a list of strings, make sure the originals stay 
 		 * around long enough.
 		 */
-		[[[OC_PythonObject alloc] initWithObject:seq] autorelease];
+		[[[OC_PythonObject alloc] initWithPyObject:seq] autorelease];
 	}
 	Py_DECREF(seq);
 	return 0;

pyobjc-core/Modules/objc/objc_util.m

 	typerepr = PyObject_Str(exc_type);
 	userInfo = [NSMutableDictionary dictionaryWithCapacity: 3];
 	[userInfo setObject:
-		[[[OC_PythonObject alloc] initWithObject:exc_type] autorelease]
+		[[[OC_PythonObject alloc] initWithPyObject:exc_type] autorelease]
 		forKey:@"__pyobjc_exc_type__"];
 	if (exc_value != NULL)
 		[userInfo setObject:
-			[[[OC_PythonObject alloc] initWithObject:exc_value] autorelease]
+			[[[OC_PythonObject alloc] initWithPyObject:exc_value] autorelease]
 			forKey:@"__pyobjc_exc_value__"];
 	if (exc_traceback != NULL)
 		[userInfo setObject:
-			[[[OC_PythonObject alloc] initWithObject:exc_traceback] autorelease]
+			[[[OC_PythonObject alloc] initWithPyObject:exc_traceback] autorelease]
 			forKey:@"__pyobjc_exc_traceback__"];
 
 	val = [NSException