I tried to build a list of dictionaries using embedded Python2.5 in the following way to append dictionaries to a list.
Py_Object *pList = PyList_New(0); for (i=0; i<MAXSIZE; i++) { Py_Object *pDict = PyDict_New(); // // build this dictionary of keys & values // if (pDict != NULL) { if (PyList_Append(pList, pDict) < 0) { printf ("Failed to append new dict to dict-list\n"); } Py_DECREF(pDict); } } In this way, I found the PyList_Append only appends the pointer of pDict to pList. After Py_DECREF(pDict) and going back to, Py_Object *pDict = PyDict_New(); for next run in loop, it gets the same pointer as last time, which finally makes all dictionaries in the list are the same as the last one. (BTW, in Python, the result is correct.) What goes wrong? Now, I have to do no Py_DECREF(pDict) in the loop, but do once at the end, when the loop is finished, like Py_Object *pDict; for (i=0; i<MAXSIZE; i++) { pDict = PyDict_New(); // // build this dictionary of keys & values // if (pDict != NULL) { if (PyList_Append(pList, pDict) < 0) { printf ("Failed to append new dict to dict-list\n"); } } } Py_DECREF(pDict); In this way, would it cause memory leak? Thanks in advance for help!
-- http://mail.python.org/mailman/listinfo/python-list