Debugging segfaults
-
Are there any ways to debug segfaults in pyqt? I keep getting them everywhere and they are just awful to debug because there is no way of telling what went wrong. I'm using faulthandler to at least find out what line of python code has caused the issue. Right now I've been stuck for hours on a segfault bug. I can't figure out whats wrong from my python code alone. Is there any way to at least get a line number in the underlying C++ code of where the error occured? Even better would be some way of stepping through the code with gdb or something like that.
-
Are there any ways to debug segfaults in pyqt? I keep getting them everywhere and they are just awful to debug because there is no way of telling what went wrong. I'm using faulthandler to at least find out what line of python code has caused the issue. Right now I've been stuck for hours on a segfault bug. I can't figure out whats wrong from my python code alone. Is there any way to at least get a line number in the underlying C++ code of where the error occured? Even better would be some way of stepping through the code with gdb or something like that.
@IDontKnowHowToCode said in Debugging segfaults:
find out what line of python code has caused the issue
Did you find the line?
You can simply debug your Python app https://realpython.com/python-debugging-pdb/ -
@IDontKnowHowToCode said in Debugging segfaults:
find out what line of python code has caused the issue
Did you find the line?
You can simply debug your Python app https://realpython.com/python-debugging-pdb/@jsulm Yeah I found the line in my python code but as I said, that doesnt help. I would like to know whether there is a way to step into the C++ code underneath to figure out what causes the segfault.
-
@jsulm Yeah I found the line in my python code but as I said, that doesnt help. I would like to know whether there is a way to step into the C++ code underneath to figure out what causes the segfault.
@IDontKnowHowToCode
Well you can always run the Python under a debugger like gdb. BUt without symbols it may not tell you much. Plus the fault may show somewhere in the Python interpreter rather than you seeing it in your code, which not help diagnose.I don't know whether a nice Python IDE/debugger like PyCharm would assist.
-
@IDontKnowHowToCode
Well you can always run the Python under a debugger like gdb. BUt without symbols it may not tell you much. Plus the fault may show somewhere in the Python interpreter rather than you seeing it in your code, which not help diagnose.I don't know whether a nice Python IDE/debugger like PyCharm would assist.
@JonB said in Debugging segfaults:
I don't know whether a nice Python IDE/debugger like PyCharm would assist.
Hi @JonB , I use PyCharm and may assure you - it won't give much value for segmenation violation debugging (but yeah, it may be convenient anyway).
@IDontKnowHowToCode , I felt the same pain as you when I faced segfaults in my Qt python code. But as I had successfully overcame them I probably may give you some advices.
Almost all (if not all) my segmentation violations were caused by absence of ownership for some object. Initially it was my fault when I ingnored optional
parent
parameter in many Qt constructors. While it is optional you shouldn't ignore it - be sure that you always have a parent for an object that you know. It helps in 95% of cases. But sometimes it is not enough. You should also not assignNone
value prematurely to some object that might be used by Qt internally (I faced it withQCamera
and related things but it happened only on Windows platform).I haven't found an efficient method to debug this things fast (and
gdb
with simbols wasn't of much help either). The most effective way that works for me - I cut pieces of my application and check is it still fails or not. It is like a binary search - you cut half of your app and see does it fail or not. If yes - you cut next half. If not - you return what you just have cut and cut only half of it :) This way you finally will get a small piece of code that fails - I believe it will give you a reason. But if not - you are welcome here, in community, with this small example :) -
@jsulm Yeah I found the line in my python code but as I said, that doesnt help. I would like to know whether there is a way to step into the C++ code underneath to figure out what causes the segfault.
@IDontKnowHowToCode by the way, you may show the line of code you found. It anyway may give some clue...
-
@JonB said in Debugging segfaults:
I don't know whether a nice Python IDE/debugger like PyCharm would assist.
Hi @JonB , I use PyCharm and may assure you - it won't give much value for segmenation violation debugging (but yeah, it may be convenient anyway).
@IDontKnowHowToCode , I felt the same pain as you when I faced segfaults in my Qt python code. But as I had successfully overcame them I probably may give you some advices.
Almost all (if not all) my segmentation violations were caused by absence of ownership for some object. Initially it was my fault when I ingnored optional
parent
parameter in many Qt constructors. While it is optional you shouldn't ignore it - be sure that you always have a parent for an object that you know. It helps in 95% of cases. But sometimes it is not enough. You should also not assignNone
value prematurely to some object that might be used by Qt internally (I faced it withQCamera
and related things but it happened only on Windows platform).I haven't found an efficient method to debug this things fast (and
gdb
with simbols wasn't of much help either). The most effective way that works for me - I cut pieces of my application and check is it still fails or not. It is like a binary search - you cut half of your app and see does it fail or not. If yes - you cut next half. If not - you return what you just have cut and cut only half of it :) This way you finally will get a small piece of code that fails - I believe it will give you a reason. But if not - you are welcome here, in community, with this small example :)@StarterKit in that case I have a tip for you, check out the python faulthandler package, it at least tells you in which lien the segfault occured. I did finally find the problem by the way and I'm glad it wasn't something completely trivial: I had a TreeView where certain items would get displayed using QStyledItemDelegates. Apparently one of these delegates had a problem which only occured in certain circumstances. The reason it took me so long to find the bug was that the actual segfault happened when I tried to set the header labels for my TreeView. I'm still not sure why the segfault occured there but at least now I know what to fix.
-
@StarterKit in that case I have a tip for you, check out the python faulthandler package, it at least tells you in which lien the segfault occured. I did finally find the problem by the way and I'm glad it wasn't something completely trivial: I had a TreeView where certain items would get displayed using QStyledItemDelegates. Apparently one of these delegates had a problem which only occured in certain circumstances. The reason it took me so long to find the bug was that the actual segfault happened when I tried to set the header labels for my TreeView. I'm still not sure why the segfault occured there but at least now I know what to fix.
@IDontKnowHowToCode , thanks for the reference, I'll try
faulthandler
next time.
But actually it was never a problem to find a line of code that raises segmentation violation it rather a problem to understand why it happens there.Thinking about your problem I would suspect that you may store a reference to delegate in some local variable that is cleaned before your window is closed (or not store it at all). This way you may still have a TreeView displayed by python garbage collector may have killed the delegate already. I.e. check that scope of your delegate variable matches the scope of your TreeView which it serves for.