diff -r -c /usr/src/orig/sems/branches/1.1/apps/ivr/Ivr.cpp ivr/Ivr.cpp
*** /usr/src/orig/sems/branches/1.1/apps/ivr/Ivr.cpp	2009-03-07 03:35:05.000000000 +0200
--- ivr/Ivr.cpp	2009-04-24 20:18:17.000000000 +0300
***************
*** 751,775 ****
  {
    PYLOCK;
  
!   AmSipReply* rep_cpy = new AmSipReply(r);
!   return IvrSipReply_FromPtr(rep_cpy);
  }
  
  PyObject * getPySipRequest(const AmSipRequest& r)
  {
    PYLOCK;
  
!   AmSipRequest* req_cpy = new AmSipRequest(r);
!   return IvrSipRequest_FromPtr(req_cpy);
  }
  
  void IvrDialog::onSipReply(const AmSipReply& r) {
!   callPyEventHandler("onSipReply","O",getPySipReply(r));
    AmB2BSession::onSipReply(r);
  }
  
  void IvrDialog::onSipRequest(const AmSipRequest& r){
!   callPyEventHandler("onSipRequest","O", getPySipRequest(r));
    AmB2BSession::onSipRequest(r);
  }
  
--- 751,777 ----
  {
    PYLOCK;
  
!   return IvrSipReply_FromPtr(new AmSipReply(r));
  }
  
  PyObject * getPySipRequest(const AmSipRequest& r)
  {
    PYLOCK;
  
!   return IvrSipRequest_FromPtr(new AmSipRequest(r));
  }
  
  void IvrDialog::onSipReply(const AmSipReply& r) {
!   PyObject* pyo = getPySipReply(r);
!   callPyEventHandler("onSipReply","(O)", pyo);
!   Py_DECREF(pyo);
    AmB2BSession::onSipReply(r);
  }
  
  void IvrDialog::onSipRequest(const AmSipRequest& r){
!   PyObject* pyo = getPySipRequest(r);
!   callPyEventHandler("onSipRequest","(O)", pyo);
!   Py_DECREF(pyo);  
    AmB2BSession::onSipRequest(r);
  }
  
diff -r -c /usr/src/orig/sems/branches/1.1/apps/ivr/IvrDialogBase.cpp ivr/IvrDialogBase.cpp
*** /usr/src/orig/sems/branches/1.1/apps/ivr/IvrDialogBase.cpp	2009-03-07 03:35:05.000000000 +0200
--- ivr/IvrDialogBase.cpp	2009-04-24 20:24:35.000000000 +0300
***************
*** 53,60 ****
        return NULL;
      }
  
!     // initialize self.invite_req
!     self->invite_req = IvrSipRequest_FromPtr(self->p_dlg->getInviteReq());
      if(!self->invite_req){
        PyErr_Print();
        ERROR("IvrDialogBase: while creating IvrSipRequest instance for invite_req\n");
--- 53,60 ----
        return NULL;
      }
  
!     // initialize self.invite_req - AmSipRequest is not owned!
!     self->invite_req = IvrSipRequest_BorrowedFromPtr(self->p_dlg->getInviteReq());
      if(!self->invite_req){
        PyErr_Print();
        ERROR("IvrDialogBase: while creating IvrSipRequest instance for invite_req\n");
diff -r -c /usr/src/orig/sems/branches/1.1/apps/ivr/IvrSipReply.cpp ivr/IvrSipReply.cpp
*** /usr/src/orig/sems/branches/1.1/apps/ivr/IvrSipReply.cpp	2009-03-07 03:35:05.000000000 +0200
--- ivr/IvrSipReply.cpp	2009-04-24 20:20:20.000000000 +0300
***************
*** 61,66 ****
--- 61,73 ----
    return (PyObject *)self;
  }
  
+ static void
+ IvrSipReply_dealloc(IvrSipReply* self) 
+ {
+   delete self->p_req;
+   self->ob_type->tp_free((PyObject*)self);
+ }
+  
  // static void
  // IvrSipRequest_dealloc(IvrSipRequest* self) 
  // {
diff -r -c /usr/src/orig/sems/branches/1.1/apps/ivr/IvrSipRequest.cpp ivr/IvrSipRequest.cpp
*** /usr/src/orig/sems/branches/1.1/apps/ivr/IvrSipRequest.cpp	2009-03-07 03:35:05.000000000 +0200
--- ivr/IvrSipRequest.cpp	2009-04-24 20:23:54.000000000 +0300
***************
*** 30,35 ****
--- 30,36 ----
      
    PyObject_HEAD
    AmSipRequest* p_req;
+   bool own_p_req;
  } IvrSipRequest;
  
  
***************
*** 56,72 ****
      }
  	
      self->p_req = (AmSipRequest*)PyCObject_AsVoidPtr(o_req);
    }
  
    DBG("IvrSipRequest_new\n");
    return (PyObject *)self;
  }
  
! // static void
! // IvrSipRequest_dealloc(IvrSipRequest* self) 
! // {
! //   self->ob_type->tp_free((PyObject*)self);
! // }
  
  #define def_IvrSipRequest_GETTER(getter_name, attr)		\
    static PyObject*						\
--- 57,108 ----
      }
  	
      self->p_req = (AmSipRequest*)PyCObject_AsVoidPtr(o_req);
+     self->own_p_req = true;
    }
  
    DBG("IvrSipRequest_new\n");
    return (PyObject *)self;
  }
+  
+ static PyObject* IvrSipRequest_newRef(PyTypeObject *type, PyObject *args, PyObject *kwds)
+ {
+   static char *kwlist[] = {"ivr_req", NULL};
+   IvrSipRequest *self;
  
!   self = (IvrSipRequest *)type->tp_alloc(type, 0);
!   if (self != NULL) {
! 	
!     PyObject* o_req = NULL;
!     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &o_req)){
! 	    
!       Py_DECREF(self);
!       return NULL;
!     }
!     
!     if ((NULL == o_req) || !PyCObject_Check(o_req)){
! 	    
!       Py_DECREF(self);
!       return NULL;
!     }
! 	
!     self->p_req = (AmSipRequest*)PyCObject_AsVoidPtr(o_req);
!     self->own_p_req = false;
!   }
! 
!   DBG("IvrSipRequest_newRef\n");
!   return (PyObject *)self;
! }
! 
! static void
! IvrSipRequest_dealloc(IvrSipRequest* self) 
! {
!   DBG("IvrSipRequest_dealloc\n");
! 
!   if(self->own_p_req)
!     delete self->p_req;
! 
!   self->ob_type->tp_free((PyObject*)self);
! }
  
  #define def_IvrSipRequest_GETTER(getter_name, attr)		\
    static PyObject*						\
***************
*** 204,206 ****
--- 240,255 ----
  
    return py_req;
  }
+ 
+ PyObject* IvrSipRequest_BorrowedFromPtr(AmSipRequest* req)
+ {
+   PyObject* c_req = PyCObject_FromVoidPtr(req,NULL);
+   PyObject* args = Py_BuildValue("(O)",c_req);
+     
+   PyObject* py_req = IvrSipRequest_newRef(&IvrSipRequestType,args,NULL);
+     
+   Py_DECREF(args);
+   Py_DECREF(c_req);
+ 
+   return py_req;
+ }
diff -r -c /usr/src/orig/sems/branches/1.1/apps/ivr/IvrSipRequest.h ivr/IvrSipRequest.h
*** /usr/src/orig/sems/branches/1.1/apps/ivr/IvrSipRequest.h	2009-03-07 03:35:05.000000000 +0200
--- ivr/IvrSipRequest.h	2009-04-24 20:24:35.000000000 +0300
***************
*** 31,35 ****
--- 31,36 ----
  
  extern PyTypeObject IvrSipRequestType;
  PyObject* IvrSipRequest_FromPtr(AmSipRequest* req);
+ PyObject* IvrSipRequest_BorrowedFromPtr(AmSipRequest* req);
  
  #endif
