我有一個文件foo.py,它調用bar.py.我在Windows上使用Python 3.5.0。Python增加列表引用計數
foo.py
def func(size = 100000):
myList = [5]*size
print(len(gc.get_referrers(myList))) #prints 1, gc is from gc module
size = testFunction_H.testFunction_API(myList) # control goes to bar.py
print(len(gc.get_referrers(myList))) #prints 2, why ?
del myList
testFunction_H = testFunction_API.init();
for i in range(0, 30):
func(size=1000000)
bar.py
This file has a class myClass whose below method is invoked from foo.py
def __call__(self, *args, **kwargs)
# args is a tuple that stores the passed list myList
print(len(gc.get_referrers(args))) #prints 1
future_tuple = self._myhandle.evaluateFunction(args) #evaluateFunction is a C++ function (in a .dll file) that I invoke from Python
print(len(gc.get_referrers(args)) #prints 1
return 5 #return some random value
我從foo.py傳遞一個列表bar.py.在bar.py裏面,我看到傳入列表bar.py的引用計數爲1,並且突然當控制返回到foo.py(通過return語句)時,列表的引用計數爲2.註釋掉對evaluateFunction的調用打印「1 「無處不在。有人可以解釋可能發生了什麼嗎?謝謝!
編輯:我真的不明白爲什麼我得到這個消極的票。我遇到的問題相當複雜,重現它將需要你額外的庫,而不是什麼。我已經很好地確定了repro代碼的範圍,並要求檢測「語義」問題不值得投票反對。
據推測,'evaluateFunction'正在創建'myList'參考......不是嗎? –
此外,'args'將是包含您的列表的一個*元組*,所以這就是爲什麼它不反映它所包含的參數的引用計數。 –
這不代表您的真實代碼。我們不能運行這個,我們不能調試這個。 – user2357112