Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Debugging segfaults
Forum Updated to NodeBB v4.3 + New Features

Debugging segfaults

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 4 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IDontKnowHowToCode
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • I IDontKnowHowToCode

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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/

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      I 1 Reply Last reply
      0
      • jsulmJ jsulm

        @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/

        I Offline
        I Offline
        IDontKnowHowToCode
        wrote on last edited by IDontKnowHowToCode
        #3

        @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.

        JonBJ S 2 Replies Last reply
        0
        • I IDontKnowHowToCode

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          S 1 Reply Last reply
          0
          • JonBJ JonB

            @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.

            S Offline
            S Offline
            StarterKit
            wrote on last edited by StarterKit
            #5

            @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 assign None value prematurely to some object that might be used by Qt internally (I faced it with QCamera 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 :)

            I 1 Reply Last reply
            0
            • I IDontKnowHowToCode

              @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.

              S Offline
              S Offline
              StarterKit
              wrote on last edited by
              #6

              @IDontKnowHowToCode by the way, you may show the line of code you found. It anyway may give some clue...

              1 Reply Last reply
              0
              • S StarterKit

                @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 assign None value prematurely to some object that might be used by Qt internally (I faced it with QCamera 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 :)

                I Offline
                I Offline
                IDontKnowHowToCode
                wrote on last edited by
                #7

                @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.

                S 1 Reply Last reply
                0
                • I IDontKnowHowToCode

                  @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.

                  S Offline
                  S Offline
                  StarterKit
                  wrote on last edited by StarterKit
                  #8

                  @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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved