13

I have a class that is a base for my other non-qt classes. This class instantiates QObject class with Signal instance. Unfortunately, sometimes it raises Segmentation Fault error. Here is my code:

class PublisherSignal(QObject):
    notify = Signal(list)


class PublisherBase:
    def __init__(self, *args, **kwargs):
        super(PublisherBase, self).__init__(*args, **kwargs)
        self._signal = PublisherSignal()

The faulthandler shows, that the segmentation fault is happening on the PublisherSignal() class instantiation. It is not always. In most cases it is working fine. No threads are involved. Subclasses of PublisherBase are not subclassing QObject.
What could be the cause of the segfaults?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Djent
  • 2,877
  • 10
  • 41
  • 66
  • What's wrong with the one I provided? It crashes when instantiating the `PublisherBase` class (which instantiates `PublisherSignal`) which are in the example. – Djent Dec 14 '18 at 07:48
  • 1
    Your example is not complete, you have only declared 2 classes but you have not used it and you should know that the problem may be caused by how you use it. So it's better that you provide a [mcve] if you want help. :-) – eyllanesc Dec 14 '18 at 12:31
  • I cannot reproduce this using python 3.7.1, qt 4.8.7 and pyside 1.2.4. However, since both qt4 and pyside are now totally obsolete, I don't see any point in trying to debug it further. You should switch to pyside2/qt5, and then If the problem still persists, you will need to [report it on the appropriate bug tracker](https://wiki.qt.io/Qt_for_Python/Reporting_Bugs). – ekhumoro Dec 16 '18 at 15:41
  • The problem is that I can't switch to PySide2 since it uses Qt 5. Due to licences, we can't use it. Also, the example is not reproducable in 100%. It mostly works, but sometimes (~1% cases) it explodes with segfault. – Djent Dec 17 '18 at 06:13
  • 1
    @Djent Well, that is what you get for relying on unsupported legacy software. Issues like this will *never* be fixed now. If you want to diagnose the problem properly, you will have to compile debug builds of at least qt4 and pyside so you can get a full stacktrace. That *might* be enough to reveal what the underlying cause of the issue is (but I wouldn't count on it). – ekhumoro Dec 17 '18 at 23:08
  • 1
    @Djent I suspect the problem lies in passing complex python types as signal parameters (as opposed to basic types like integers and strings). If so, one possible work-around might be to send the parameters as json strings - or perhaps just try to rewrite your code so there's never any need to send types like `list` and `dict`. – ekhumoro Dec 17 '18 at 23:08
  • For a moment I was thinking that changing the type from list to str actually helped, but unfortunately the problem persists. I guess I will need to investigate options to move to PySide2 in this case. – Djent Dec 21 '18 at 07:50
  • @Djent It seems highly suspicious that merely emiiting a custom signal with a `str` parameter would cause a segfault. There are an awful lot of pyside programs around that have never had that problem, so you must be doing something different elsewhere in your code. You really should post a complete, minimal example that is known to reproduce the problem. The code currently in your question clearly cannot do that. – ekhumoro Dec 22 '18 at 17:46

1 Answers1

4

First: Segmentation fault is a complicated issue for developers. To effectively handle it use fault handler. It is a part of Python v.3.x but you could install it in Python v.2.x using pip. But sometimes you'd better use event filter Register – for a widget to track signal events for. Here's an example for mouse (it's just to see how it looks like):

# IT IS JUST AN EXAMPLE (NOT A SOLUTION)

def eventFilter(self, source, event):
    if event.type() == QEvent.MouseButtonPress:
        if source == self.txtEditor :
            pos=event.pos()
            cursor=self.txtEditor.cursorForPosition(pos)
            cursor.select(QTextCursor.WordUnderCursor)
            txtClicked=cursor.selectedText()
            self.testCommand(str(txtClicked))
    return QMainWindow.eventFilter(self, source, event)

Second: You might use The Python Debugger module:

python -m pdb yourScript.py

Third: For threads involved (but you said that no threads are involved).

shutdown (exit) can hang or segfault with daemon threads running
mckade.charly
  • 65
  • 2
  • 17
  • I am using faulthandler, and it is telling me that the segmentation fault is caused by the instantiation of the `PublisherSignal` object. No idea why. – Djent Dec 17 '18 at 06:07