Parcourir la source

some incref/memory issues
made s_Question also use a shared_ptr


git-svn-id: svn://bind10.isc.org/svn/bind10/experiments/python-binding@1847 e5f2f494-b856-4b98-b285-d166d9295462

Jelte Jansen il y a 15 ans
Parent
commit
dc8e0e7e50

+ 3 - 8
src/lib/dns/python/message_python.cc

@@ -1490,10 +1490,8 @@ Message_init(s_Message* self, PyObject* args)
         PyErr_Clear();
         if (i == Message::PARSE) {
             self->message = new Message(Message::PARSE);
-            Py_INCREF(self);
             return 0;
         } else if (i == Message::RENDER) {
-            Py_INCREF(self);
             self->message = new Message(Message::RENDER);
             return 0;
         } else {
@@ -1711,7 +1709,7 @@ Message_getQuestion(s_Message* self)
          ++qi) {
         s_Question *question = (s_Question*)question_type.tp_alloc(&question_type, 0);
         if (question != NULL) {
-            question->question = new Question(*qi->get());
+            question->question = *qi;
             if (question->question == NULL)
               {
                 Py_DECREF(question);
@@ -1768,10 +1766,8 @@ Message_addQuestion(s_Message* self, PyObject* args)
         return NULL;
     }
 
-    Py_INCREF(question);
-    QuestionPtr question_ptr = QuestionPtr(question->question);
-    self->message->addQuestion(question_ptr);
-
+    self->message->addQuestion(question->question);
+    
     Py_RETURN_NONE;
 }
 
@@ -1786,7 +1782,6 @@ Message_addRRset(s_Message* self, PyObject* args)
                                            &PyBool_Type, &sign)) {
         return NULL;
     }
-    Py_INCREF(rrset);
     
     if (sign == Py_True) {
         self->message->addRRset(*section->section, rrset->rrset, true);

+ 6 - 8
src/lib/dns/python/question_python.cc

@@ -38,7 +38,7 @@ using namespace isc::dns;
 // The s_* Class simply coverst one instantiation of the object
 typedef struct {
     PyObject_HEAD
-    Question* question;
+    QuestionPtr question;
 } s_Question;
 
 //
@@ -153,14 +153,14 @@ Question_init(s_Question* self, PyObject* args)
                                                &rrclass_type, &rrclass,
                                                &rrtype_type, &rrtype
            )) {
-            self->question = new Question(*name->name, *rrclass->rrclass,
-                                          *rrtype->rrtype);
+            self->question = QuestionPtr(new Question(*name->name, *rrclass->rrclass,
+                                          *rrtype->rrtype));
             return 0;
         } else if (PyArg_ParseTuple(args, "y#|I", &b, &len, &position)) {
             PyErr_Clear();
             InputBuffer inbuf(b, len);
             inbuf.setPosition(position);
-            self->question = new Question(inbuf);
+            self->question = QuestionPtr(new Question(inbuf));
             return 0;
         }
     } catch (isc::dns::DNSMessageFORMERR dmfe) {
@@ -177,7 +177,7 @@ Question_init(s_Question* self, PyObject* args)
         return -1;
     }
 
-    self->question = NULL;
+    self->question = QuestionPtr();
     
     PyErr_Clear();
     PyErr_SetString(PyExc_TypeError,
@@ -188,9 +188,7 @@ Question_init(s_Question* self, PyObject* args)
 static void
 Question_destroy(s_Question* self)
 {
-    if (self->question != NULL)
-        delete self->question;
-    self->question = NULL;
+    self->question.reset();
     Py_TYPE(self)->tp_free(self);
 }
 

+ 3 - 0
src/lib/dns/python/rdata_python.cc

@@ -150,6 +150,9 @@ Rdata_init(s_Rdata* self, PyObject* args)
 static void
 Rdata_destroy(s_Rdata* self)
 {
+    // Clear the shared_ptr so that its reference count is zero
+    // before we call tp_free() (there is no direct release())
+    self->rdata.reset();
     Py_TYPE(self)->tp_free(self);
 }
 

+ 3 - 1
src/lib/dns/python/rrset_python.cc

@@ -150,7 +150,6 @@ RRset_init(s_RRset* self, PyObject* args UNUSED_PARAM)
                                            &rrtype_type, &rrtype,
                                            &rrttl_type, &rrttl
        )) {
-        Py_INCREF(self);
         self->rrset = RRsetPtr(new RRset(*name->name, *rrclass->rrclass,
                                 *rrtype->rrtype, *rrttl->rrttl));
         return 0;
@@ -163,6 +162,9 @@ RRset_init(s_RRset* self, PyObject* args UNUSED_PARAM)
 static void
 RRset_destroy(s_RRset* self)
 {
+    // Clear the shared_ptr so that its reference count is zero
+    // before we call tp_free() (there is no direct release())
+    self->rrset.reset();
     Py_TYPE(self)->tp_free(self);
 }