Skip to main content
 首页 » 编程设计

python 3 : Crash with free(): corrupted unsorted chunks while reading file

2024年10月17日8emanlee

我发现 Python 3 有以下奇怪的行为:

file = open(path, mode='rb').read() 
file_ori = open(self.filePath, mode='rb').read() 
 
m = hashlib.md5() 
md5 = m.update(file) 
md5 = m.hexdigest() 
 
file = '0x'.encode('ascii') + binascii.hexlify(file) 
file_ori = '0x'.encode('ascii') + binascii.hexlify(file_ori) 

在这种情况下,我的应用程序迟早会因 free(): corrupted unsorted chunks 而崩溃。

如果我将代码更改为:

file = open(path, mode='rb') 
file_ori = open(self.filePath, mode='rb') 
 
filecont = file.read() 
file_oricont = file_ori.read() 
 
m = hashlib.md5() 
md5 = m.update(filecont) 
md5 = m.hexdigest() 
 
fileb = '0x'.encode('ascii') + binascii.hexlify(filecont) 
fileb_ori = '0x'.encode('ascii') + binascii.hexlify(file_oricont) 

应用程序运行正常。

我多次阅读文档,但找不到导致此行为的任何原因。有人可能对此了解更多吗?在我看来,Python 和垃圾收集器存在错误?

我用python3.4和python3.5测试过

谢谢 曼纽尔

请您参考如下方法:

第一个例子:在 python 2.7 中测试有效并告诉我错误发生在哪一行

第二个例子:你必须知道当你调用 f.read() 时,文件中的光标到达文件的末尾,所以另一个 f.read() 将不输出任何内容,即 ''所以这两个例子是不同的