Hi Vincent!
Because it’s a broad question, I’ll provide a broad answer.
This is most likely the result of reloading without freeing resources claimed by Qt. When you reload, Python does a good job garbage collecting all of its items, but it cannot know the order in which Qt’s objects should be freed. In this case, it’s likely the assigned widget to your proxy need to be cleaned up first; but as Python doesn’t know of this relationship, it cannot take that information into account during garbage collection, which would in that case delete something that Qt then tried to delete again, which in C++ land would be undefined. Undefined means it may crash, or it may not. Which means the first 3 times you try, you’re in luck. And on the 4th you’re not. Undefined doesn’t necessarily mean random though, so it may even consistently not-crash the first 3, and crash consistently on the 4th.
You can avoid situations like this altogether with a little programming hygene. What I typically do is two things.
reload()
, free with sys.modules.pop
The uninstall in this case could consist of un-assigning the widgets to the proxy before freeing. Qt is able to automatically free objects if they form a hierarchy, like when you assign widgets to a layout. I would expect it to also handle cases where you assign a widget like in your example, but odds are it doesn’t in which case you need to do it yourself.
Generally, I structure my projects into an install()
and uninstall()
function, where uninstall()
does everything install()
does in reverse; including making connections to databases, or creating menus in Maya or loading images and fonts off of disk etc. On calling uninstall()
I expect a Maya in the exact same condition that it was when I first called install()
. This could happen either at a package, module and/or class level.
Freeing with sys.modules.pop("mymodule")
is relevant when you work with multiple Python modules that reference each other. You’ve probably already found that if you reload a module that imports another module, that imported module isn’t reloaded. It would hold onto any references it made - including widgets that the parent module made use of - which means the module you reload would re-import that persistent module and thus gain access to whatever data it previously held. You could work around it by reloading that module too, but the issue arises when modules form a hierarchy and worse yet when there’s diamond or cyclic dependency between modules.
In that case you’ve probably also found that the order in which you reload becomes important and then down the rabbit hole you go.
To solve this you can instead divide the work of unloading and loading by removing entries from sys.modules
.
# For example
import mymodule
import sys
for module in list(sys.modules):
if module.startswith("mymodule"):
sys.modules.pop(module)
import mymodule
The .pop()
has the same effect as reload, except it doesn’t automatically re-import it, the .startswith
ensures that any sub-modules are taken into account as well. Then when you next import, it will import it in whatever order it was imported the first time around. This also guarantees that the modules you’ve now imported won’t contain any references from before and that it’s an entirely clean slate.
It will also make apparent which of those references you were dependent on and throw lots more errors due to things actually disappearing and not lingering. These are the errors you want to get rid of.
Somewhat general advice, but having followed this method for many years I haven’t had these issues when iterating on modules or packages in Maya (or any embedded environment) and certainly no crashes because of it.
For more specific advice about this specific crash, maybe try and replicate it in mayapy, in e.g. a loop that imports it over and over until it crashes. Such that you can cause a crash fast and consistently. Once you’ve got it, reduce it down to the few lines that cause it and take things from there.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/318b7121-df53-4743-9355-a1946d6e3da5%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/318b7121-df53-4743-9355-a1946d6e3da5%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/a0892932-833a-4986-9ac2-c1f7b84da110%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1cVz8vcVAFL8x2wEp5TgB%3DTtftAMJFQfSDQuUg%3DipWjw%40mail.gmail.com.