<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7651.59">
<TITLE>list of dictionary in embedded Python</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->
<P><FONT SIZE=2>I tried to build a list of dictionaries using embedded Python2.5<BR>
in the following way to append dictionaries to a list.<BR>
<BR>
Py_Object *pList = PyList_New(0);<BR>
<BR>
for (i=0; i<MAXSIZE; i++) {<BR>
Py_Object *pDict = PyDict_New();<BR>
//<BR>
// build this dictionary of keys & values<BR>
//<BR>
if (pDict != NULL) {<BR>
if (PyList_Append(pList, pDict) < 0) {<BR>
printf ("Failed to append new dict to dict-list\n");<BR>
}<BR>
Py_DECREF(pDict);<BR>
}<BR>
}<BR>
<BR>
In this way, I found the PyList_Append only appends the pointer<BR>
of pDict to pList.<BR>
<BR>
After Py_DECREF(pDict) and going back to,<BR>
Py_Object *pDict = PyDict_New();<BR>
for next run in loop, it gets the same pointer as last time,<BR>
which finally makes all dictionaries in the list are the same<BR>
as the last one. (BTW, in Python, the result is correct.)<BR>
<BR>
What goes wrong?<BR>
<BR>
Now, I have to do no Py_DECREF(pDict) in the loop, but do once at<BR>
the end, when the loop is finished, like<BR>
<BR>
Py_Object *pDict;<BR>
for (i=0; i<MAXSIZE; i++) {<BR>
pDict = PyDict_New();<BR>
//<BR>
// build this dictionary of keys & values<BR>
//<BR>
if (pDict != NULL) {<BR>
if (PyList_Append(pList, pDict) < 0) {<BR>
printf ("Failed to append new dict to dict-list\n");<BR>
}<BR>
}<BR>
}<BR>
Py_DECREF(pDict);<BR>
<BR>
In this way, would it cause memory leak?<BR>
<BR>
Thanks in advance for help!<BR>
<BR>
<BR>
<BR>
</FONT>
</P>
</BODY>
</HTML>