- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到一个问题,即保存导入的 python 模块的对象被损坏,但仅在显式调用 sys.exit() 后才损坏。这也是间歇性的。除了“标准”pyusb 模块(我使用了该模块的最新版本)之外,我已经删除了大部分代码。实际上发生的事情很少。
如果程序调用 sys.exit(),则某些对象 ID 会更改为垃圾值。如果我忽略它,那么就不会有错误!
这真的是 Python 中的一个错误吗?这似乎不太可能。我做错了什么?
====主程序“bug_happens.py”
#!/usr/bin/python3
import sys
import usbtmc
scope = usbtmc.Instrument()
print('in Main')
print(' id of usbtmc.usb: 0x{:x}'.format(id(usbtmc.usb)))
print(' id of usbtmc.usb.util: 0x{:x}'.format(id(usbtmc.usb.util)))
print(' id of usbtmc.usb.util.dispose_resources: 0x{:x}'.format(id(usbtmc.usb.util.dispose_resources)))
print()
print('calling Exit()')
sys.exit()
===== 非常精简的 usbtmc 模块
"""
****** Very stripped down partial Version for debugging a memory corruption issue ******
Python USBTMC driver
Copyright (c) 2012-2017 Alex Forencich
...
"""
import usb.util
class Instrument(object):
def __init__(self, *args, **kwargs):
self.connected = False
print('usbtmc.Instrument created 0x{:x}'.format(id(self)))
print(' id of usb: 0x{:x}'.format(id(usb)))
print(' id of usb.util: 0x{:x}'.format(id(usb.util)))
print(' id of usb.util.dispose_resources: 0x{:x}'.format(id(usb.util.dispose_resources)))
print()
def __del__(self):
print('usbtmc.Instrument.__del__() called for 0x{:x}'.format(id(self)))
print(' id of usb: 0x{:x}'.format(id(usb)))
print(' id of usb.util: 0x{:x}'.format(id(usb.util)))
print(' id of usb.util.dispose_resources: 0x{:x}'.format(id(usb.util.dispose_resources)))
print(' end of usbtmc.Instrument.__del__()')
print()
================================================== =======
usbtmc.Instrument created 0x7fee5775d9b0
id of usb: 0x7fee562800e8
id of usb.util: 0x7fee562247c8
id of usb.util.dispose_resources: 0x7fee561ad048
in Main
id of usbtmc.usb: 0x7fee562800e8
id of usbtmc.usb.util: 0x7fee562247c8
id of usbtmc.usb.util.dispose_resources: 0x7fee561ad048
calling Exit()
usbtmc.Instrument.__del__() called for 0x7fee5775d9b0
id of usb: 0x7fee562800e8
id of usb.util: 0x7fee562247c8
id of usb.util.dispose_resources: 0x7fee561ad048
end of usbtmc.Instrument.__del__()
================================================== =======
usbtmc.Instrument created 0x7fec34b829b0
id of usb: 0x7fec336c3278
id of usb.util: 0x7fec335dbb88
id of usb.util.dispose_resources: 0x7fec335e5048
in Main
id of usbtmc.usb: 0x7fec336c3278
id of usbtmc.usb.util: 0x7fec335dbb88
id of usbtmc.usb.util.dispose_resources: 0x7fec335e5048
calling Exit()
usbtmc.Instrument.__del__() called for 0x7fec34b829b0
id of usb: 0xa40060
Exception ignored in: <bound method Instrument.__del__ of <usbtmc.Instrument object at 0x7fec34b829b0>>
Traceback (most recent call last):
File "/home/don/Electronics/Projects/HF_Meas/Scope_SA/Bug_usb_close/usbtmc.py", line 27, in __del__
AttributeError: 'NoneType' object has no attribute 'util'
================================================== =======
usbtmc.Instrument created 0x7ff75968c9b0
id of usb: 0x7ff7581af0e8
id of usb.util: 0x7ff7581537c8
id of usb.util.dispose_resources: 0x7ff7580dc048
in Main
id of usbtmc.usb: 0x7ff7581af0e8
id of usbtmc.usb.util: 0x7ff7581537c8
id of usbtmc.usb.util.dispose_resources: 0x7ff7580dc048
calling Exit()
usbtmc.Instrument.__del__() called for 0x7ff75968c9b0
id of usb: 0x7ff7581af0e8
id of usb.util: 0xa40060
Exception ignored in: <bound method Instrument.__del__ of <usbtmc.Instrument object at 0x7ff75968c9b0>>
Traceback (most recent call last):
File "/home/don/Electronics/Projects/HF_Meas/Scope_SA/Bug_usb_close/usbtmc.py", line 28, in __del__
AttributeError: 'NoneType' object has no attribute 'dispose_resources'
在 Mint 18.3 系统上运行 python3 3.5.1-3以及 python3-usb 1.0.0~b2-2 或 pyusb-1.0.2 的新 git pull
最佳答案
当Python解释器退出时,运行时将被拆除,并且对象删除的顺序不是您可以依赖的。事实上,甚至不能保证解释器退出时会调用 __del__()
方法。
自定义 __del__
需要访问的对象(包括其他模块)可能已被删除或设置为 None
,这将导致您所看到的异常这里('NoneType'对象没有属性...)
这并不是真正的“内存损坏”,它只是为 Instrument
类编写的糟糕代码。任何必要的设置/拆卸上下文都应使用 __enter__
和 __exit__
进行管理,因为 __del__
不适合此目的。所以,这个问题或多或少是 Instrument
中的一个错误。可以通过利用记录在案的数据模型保证来改进此代码 here :
Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the
__del__()
method is called.
关于python - 退出时内存损坏(或愚蠢的用户错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50574201/
我目前正在为 Cocoa/Objective-C 项目编写一个脚本来完全自动化我的编译-运行-调试过程。 我的最后一行代码是: lldb -f Build/MyApp.app -o "run" 这实际
我有一个带有登录屏幕的脚本,如果按下取消按钮,我想完全退出该应用程序。我尝试了 3 种方法: 系统退出() QApplication.quit() QCoreApplication.instance(
我有一个 Flash 应用程序,可以重定向到另一个页面。我很乐意捕获任何其他窗口卸载事件(单击链接/提交表单)并警告用户他们将丢失 Flash 应用程序中的进度。 但是,我找不到任何方法来判断 URL
我正在尝试在 Ubuntu 上用 Python 编写一个简单的程序,它将在播放视频完成后关闭/退出/退出 VLC Player。 能否请您指导我应该在我的程序中添加什么以获得我需要的结果。 impor
我在 Lynda.com 上学习 PHP 2 视频时遇到了一个问题,因为讲师似乎忽略了告诉我们他在视频中执行的步骤之一。我在这里上传了相关视频http://www.youtube.com/watch?
某天在群内有同学问到,在python下我用input或者raw_input都得输入完后回车才能获取到输入的值,那如何实现任意键退出暂停等功能呢,我当时也没有多想,因为接触python时间也不算长,主
我按顺序调用了几个函数(我无法编辑),但有些函数会重定向用户,所以我永远不会进入下一个函数。 我正在调用一个第三方函数,它调用了我能够阻止的 wp_redirect(),但是下一行是 exit;我不知
终止/退出主函数的 D 方式是什么? import std.stdio; import core.thread; void main() { int i; while (i <= 5)
我正在申请写作。用户可以打开应用程序、写一些文本、保存他们的工作等。 我试图做到这一点,以便点击 window close按钮将提示用户 (a) 保存他们的工作(如有必要)或 (b) 退出。 我正在尝
我正在通过在 repl 中检查别人的代码来玩弄它。 它不断调用 System/exit,这导致我的 repl 崩溃。这真是令人气愤。 在我有权访问的所有代码中,我都模拟了调用。 但它也会调用一些我没有
我正在使用 subprocess执行mimic的模块程序(指定 here )。下面的代码成功地读取了一些文本并写入了一个 mp3 文件。 import subprocess proc = subpro
退出 .then 范围后数组上的值被清除 在下面的代码中tableValues1.length 给我正确的长度,直到它位于每个循环内当它退出时,作用域数组长度为零。 请谁能帮我解决这个问题 - 谢谢
我正在尝试为 s3cmd 编写一个 docker 镜像。当我通过 docker-compose 运行从 Dockerfile 构建的图像时,容器在 docker compose run 命令之前退出。
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: Quitting an application - is that frowned upon? 我编写了一个 And
我遇到 Selenium WebDriver 仅执行一次后退出 for 循环的问题。据推测,这是获取内容和在加载页面之前启动循环的问题。是否有可能让 webdriver 等待页面加载? List al
#include #include #include #include "Player.h" using namespace std; void PlayerMenu(); int main()
class Test{ public static void main(String args[]) { Patron list[] = new PatronData(
我正在做一些作业,遇到了这个问题。 Write a program that reads several lines of text and prints a table indicating the
我正在用 C 创建一个简单的 Linux 命令 shell。我无法理解我的代码在哪里出现问题。 “commands”是我希望作为一个父进程的子进程同时执行的 Linux 命令字符串列表。当所有执行完成
我的控制台应用程序有点问题。应用程序应该从用户那里获取数字并将它们添加到列表中,但是如果输入是“c”,它应该关闭。我不知道如何在不使用 Scanner.nextLine() 挂起应用程序并退出循环的情
我是一名优秀的程序员,十分优秀!