Destructor never called ???

vb muk_pr at yahoo.com
Wed Sep 11 18:50:24 EDT 2002


Hi, 
  I have the following code my problem is when I call any methods of  
       TCVector_Object the destructor never executes.

I mean

v = module.CreateTCVector()
del v;
//destructor runs. message shown .OK

but 
in

v = module.CreateTCVector()
v.Append(123);
del v;//or closing python interpreter

destructor never gets called. Any ideas?

BTW are there any extending&embedding docs (except ASPN & Python docs)
I realy have hard times in that ref. counting stuff and
is my way the official way of adding methods to a new type ?

Thanks.



headers.............

staticforward PyTypeObject TCVector_Type;
staticforward PyMethodDef TCVector_methods[];

struct TCVector_Object{
    PyObject_HEAD
    vector<int> List;
};

static PyObject*
new_TCVector(PyObject* self, PyObject* args)
{
    TCVector_Object *obj;

    if (!PyArg_ParseTuple(args,""))
        return NULL;

    obj = PyObject_New(TCVector_Object, &TCVector_Type);
    return (PyObject*)obj;
}

static void
delete_TCVector(PyObject* obj)
{
    ShowMessage("Deallocating");
    PyObject_Del(obj);
}

static PyObject *
TCVector_getattr(PyObject *obj, char *name)
{   //Methods will be looked here.
    AnsiString attrname = name;
    if (attrname == "Count")
    {
        return Py_BuildValue("i",((TCVector_Object*)obj).size());
    }
    else
        return Py_FindMethod(TCVector_methods, obj, name);
}

static PyObject *
TCVector_Append(PyObject* self, PyObject* args)
{
  int i;
  if (!PyArg_ParseTuple(args,"i",&i))
  {
    return NULL;
  }
  ((TCVector_Object*)self).push_back(i);
  Py_INCREF(Py_None);
  return Py_None;
}

static PyObject *
TCVector_Clear(PyObject* self, PyObject* args)
{
  ((TCVector_Object*)self).clear();
  Py_INCREF(Py_None);
  return Py_None;
}

static PyTypeObject TCVector_Type = {
    PyObject_HEAD_INIT(&PyType_Type)
    0,
    "TCVector",
    sizeof(TCVector_Object),
    0,
    delete_TCVector, /*tp_dealloc*/
    0,               /*tp_print*/
    TCVector_getattr,          /*tp_getattr*/
    0,          /*tp_setattr*/
    0,          /*tp_compare*/
    0,          /*tp_repr*/
    0,          /*tp_as_number*/
    0,          /*tp_as_sequence*/
    0,          /*tp_as_mapping*/
    0,          /*tp_hash */
};

static PyMethodDef TCVector_methods[] = {
    {"Append", (PyCFunction)TCVector_Append, METH_VARARGS,""},
    {"Clear", (PyCFunction)TCVector_Clear, METH_VARARGS,""},
    {NULL, NULL, 0, NULL}           /* sentinel */
};


static PyMethodDef samplemodule_methods[] = {
    {"CreateTCVector", new_TCVector, METH_VARARGS,
     "Create a new TCVector object."},
    {NULL, NULL, 0, NULL}
};

void __export __stdcall initsamplemodule()
{
    Py_InitModule("samplemodule", borland_tools_methods);
}



More information about the Python-list mailing list