-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should Instrument.__del__() and close() not exist? #44
Comments
Is that actually the correct solution? Just don't do any clean up at all and hope things will just work out? Seems like the linked question says to use |
So, it looks like I also found https://stackoverflow.com/questions/36981177/python-with-statement-in-gui-pyqt-program , which seems to indicate that it may not be possible for python to do this sort of cleanup properly. |
Hmm, I also found atexit, maybe that's a way to get some of this manual cleanup sorted out properly. |
Looks like atexit might be ideal, with the one unfortunate problem being atexit does not support unregister in python 2, so some sort of callable weak reference is required for python 2 compatibility. This could solve a number of issues, though, for more than just python-usbtmc. |
The place where I had problems was when close called usb.util.dispose_resources. It looks like pyusb goes to a lot of effort to support explicit freeing of resources, but as it notes, Python does not really provide for this. I think one of the limitations of using a language that hides all of its memory management is that you have to trust it to handle things for you. Instead of freeing an object you have to clear anything that refers to it and the runtime should then free it. |
I also wonder why I seem to be the only one seeing this. Am I doing something that different??? |
I have run in to some weirdness with python-usbtmc, but I always chalked it up to instruments not implementing the protocol correctly. I think you're not the only one who has seen this issue, but you're the first so far to figure out what's been going on. I have had issues with python-vxi11 not properly closing links leading to instruments (namely certain Tektronix oscilloscopes) that don't free links when the TCP connection is closed to run out of resources and require a reboot and added the |
Also, would you mind trying something? Add
at the top, and
at the bottom of |
Using atexit prevents errors in a simple test where I just open and then call exit. If I access the device to read data then I still get warnings like this from down in pyUSB. Exception ignored in: <bound method Device.del of <DEVICE ID 1ab1:04ce on Bus 003 Address 042>> Or like this: Exception ignored in: <bound method _Device.del of <usb.backend.libusb1._Device object at 0x7fad44d4b390>> |
I have been getting somewhat random python errors due to dereferencing deleted objects. The answer seems to be not to have either __del__() or close() functions in the Instrument class (and thus not call usb.util.dispose_resources()) because Python deletes things in unpredictable orders. Just calling close() causes the same problems even with __del__() removed.
See: https://stackoverflow.com/questions/50574201/memory-corruption-on-exit-or-stupid-user-error
The text was updated successfully, but these errors were encountered: