Virgil Dupras avatar Virgil Dupras committed 75cf364

p2o now optionally doesn't retain the reference object.

Comments (0)

Files changed (1)

 typedef struct {
     PyObject_HEAD
     %%typedecl%%objc_ref;
+    unsigned char is_retained;
 } %%clsname%%_Struct;
 
 static PyTypeObject %%clsname%%_Type; /* Forward declaration */
 static void
 %%clsname%%_dealloc(%%clsname%%_Struct *self)
 {
-    [self->objc_ref release];
+    if (self->is_retained) {
+        [self->objc_ref release];
+    }
     Py_TYPE(self)->tp_free((PyObject *)self);
 }
 
 %%clsname%%_init(%%clsname%%_Struct *self, PyObject *args, PyObject *kwds)
 {
     PyObject *pRefCapsule = NULL;
-    if (!PyArg_ParseTuple(args, "|O", &pRefCapsule)) {
+    unsigned char should_retain = 1;
+    if (!PyArg_ParseTuple(args, "|Ob", &pRefCapsule, &should_retain)) {
         return -1;
     }
     
     if (pRefCapsule == NULL) {
         self->objc_ref = %%objc_create%%
+        self->is_retained = 1;
     }
     else {
         self->objc_ref = PyCapsule_GetPointer(pRefCapsule, NULL);
-        [self->objc_ref retain];
+        self->is_retained = should_retain;
+        if (should_retain) {
+            [self->objc_ref retain];
+        }
     }
     
     return 0;
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.