如何正确管理这个PyObject *数组的内存? (C扩展名) [英] How properly manage memory of this PyObject* array?? (C extension)

查看:195
本文介绍了如何正确管理这个PyObject *数组的内存? (C扩展名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个C扩展本地构建了一个PyObject *的数组,因为

跟随...


my_array = malloc(n * sizeof( PyObject *));

for(i = 0; i< n; i ++){

my_array [i] = PyList_New(0);

}


Q1:在退出此C扩展函数之前,我必须对所有元素

执行Py_DECREF(my_array [i])吗? (如果元素在其他对象中被使用了怎么办?)


Q2:我必须自由(my_array);在功能结束?如果

my_array [i]的内容是

如果在其他对象中使用,那么我就不一定只需要b / b
就可以了! !!


克里斯

Suppose a C extension locally built an array of PyObject* ''s as
follows...

my_array = malloc(n * sizeof(PyObject*));
for (i = 0; i < n; i++) {
my_array[i] = PyList_New(0);
}

Q1: Must I do a Py_DECREF(my_array[i]) on all elements
before exiting this C extension function? (What if
the elements got used in other objects?)

Q2: Must I do free(my_array); at end of function?? What if
my_array[i]''s are
used in other objects so that I can''t necessarily just
nuke it!!!

Chris

推荐答案

se ****** @ spawar.navy.mil 写道:
se******@spawar.navy.mil wrote:

假设一个C扩展本地构建了一个PyObject *的数组,如下所示......


my_array = malloc(n * sizeof(PyObject *));

for(i = 0; i< n; i ++){

my_array [i] = PyList_New(0);

}


Q1:在退出此C扩展函数之前,我必须对所有元素

执行Py_DECREF(my_array [i])吗?
Suppose a C extension locally built an array of PyObject* ''s as
follows...

my_array = malloc(n * sizeof(PyObject*));
for (i = 0; i < n; i++) {
my_array[i] = PyList_New(0);
}

Q1: Must I do a Py_DECREF(my_array[i]) on all elements
before exiting this C extension function?



如果你在现有之前释放my_array,是的。

if you''re releasing my_array before existing, yes.


(如果元素得到了怎么办?在其他对象中使用?)
(What if the elements got used in other objects?)



如果程序的其他部分存储了指向

数组元素的指针,那些部分必须确保当

复制指针时增加引用计数。


这是整个引用计数点,当然:

一个对象应该在任何时候都匹配你的程序对该对象的* active * references

的数量。

if other parts of your program storing pointers to the elements in your
array, those parts must make sure to increment the reference count when
copying the pointer.

that''s the whole point of reference counting, of course: the count for
an object should, at all times, match the number of *active* references
your program has to that object.


Q2:我必须自由(my_array);在功能结束?
Q2: Must I do free(my_array); at end of function??



除非你的程序的其他部分保留它,当然你必须释放它b
。这有点令人惊讶,你必须要问这个,

真的 - 任何C教程都应该解释malloc / free是如何工作的。

unless some other part of your program holds on to it, of course you
have to release it. it''s a bit surprising that you have to ask this,
really -- any C tutorial should explain how malloc/free works.


如果my_array [i]在其他物体中被使用,那么我就不能用b $ b b必然只需要它!
What if my_array[i]''s are used in other objects so that I can''t
necessarily just nuke it!!!



如果那些其他对象进行了正确的引用计数,那么一切都将在b
找到工作。如果他们不这样做,你的程序迟早会崩溃,不会在你分配my_array的函数中做什么。


< / F>

if those other objects do proper reference counting, everything will
work find. if they don''t, your program will crash sooner or later, no
matter what you do in the function that allocates my_array.

</F>



Q2:我必须免费(my_array) ;在功能结束?
Q2: Must I do free(my_array); at end of function??



,除非你的程序的其他部分持有它

unless some other part of your program holds on to it



F.


谢谢!如果我理解正确,那么我从来没有

来做免费(my_array);因为my_array的所有元素

仍在使用并附加到其他地方的其他

结构中吗?


只要每个my_array的元素被管理

正确并且正确释放没有任何

理由担心免费(my_array)

永远不会得到的事实跑吧?

cs

F.

Thanks! If I understand you correctly then I never have
to do free(my_array); because all the elements
of my_array are still being used and appended to other
structures elsewhere right?

As long as each element of my_array is managed
properly and freed properly there is NEVER any
reason to worry about fact that free(my_array)
will never get run right?
cs


On 11/07/2006 4:39 AM, se ****** @ spawar.navy.mil 写道:
On 11/07/2006 4:39 AM, se******@spawar.navy.mil wrote:

>> Q2:我必须自由(my_array);在功能结束?
>>Q2: Must I do free(my_array); at end of function??


,除非你的程序的其他部分保留它

unless some other part of your program holds on to it



F.


谢谢!如果我理解正确,那么我从来没有

来做免费(my_array);因为my_array的所有元素仍然被使用并附加到其他地方的其他

结构中吗?


F.

Thanks! If I understand you correctly then I never have
to do free(my_array); because all the elements
of my_array are still being used and appended to other
structures elsewhere right?



*错误* - 重读/ F'的回复:

"""

除非你的程序的其他部分持有它,当然你要b $ b必须释放它。这有点令人惊讶,你必须问这个,

真的 - 任何C教程都应该解释malloc / free是如何工作的。

"""

*WRONG* -- reread /F''s response:
"""
unless some other part of your program holds on to it, of course you
have to release it. it''s a bit surprising that you have to ask this,
really -- any C tutorial should explain how malloc/free works.
"""


>

只要my_array的每个元素都被正确管理,并且正确地释放了
,那么从来没有任何

有理由担心免费(my_array)

永远无法运行的事实?
>
As long as each element of my_array is managed
properly and freed properly there is NEVER any
reason to worry about fact that free(my_array)
will never get run right?



* WRONG * - 担心的原因是

my_array本身占用的内存永远不会被回传。如果你没有释放它,重复

调用你的功能将导致你的应用程序内存不足。


这很简单,真的:你malloc它,你释放它。我分享了

effbot'的惊喜:如果你(或纳税人!)为一个

课程/书/教程付了钱而没有包括那个建议,

退款申请明确表示。


HTH,

John

*WRONG* -- the reason to worry is that the memory occupied by the
my_array itself is never handed back. If you don''t free it, repeated
calls to your function will cause your app to run out of memory.

It''s quite simple, really: You malloc it, you free it. I share the
effbot''s surprise: If you (or the taxpayers!) paid money for a
course/book/tutorial that didn''t include that advice, a claim for a
refund is definitely indicated.

HTH,
John


这篇关于如何正确管理这个PyObject *数组的内存? (C扩展名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆