pyotherside
pyotherside copied to clipboard
Segmentation fault when using QObject in Python
Initial comments were wrong. See later comments.
Do you call it from a different thread? Can you create a SSCCE to reproduce this issue? Also, on which version / platform / OS are you experiencing this issue? Can you run it in gdb or lldb (or any other debugger) and see where it crashes?
I'm attempting to narrow this down to a minimal sample that reproduces it but figured I'd give some info that might be related or might not.
I have a translation class that is a QObject that I use for gettext translations. I set it as a context property and use it throughout QML and to allow the same pipeline for Python I used Pyotherside to store the method in Python for use there.
QML setting the QObject to find the function from:
call_sync('translator.set_translator', [applicationWindow])
QML translate function:
function translate(text) {
return translator.getTranslation(text)
}
main.cpp:
engine.rootContext()->setContextProperty("translator", &translationService);
translation_service.cpp:
QString TranslationService::getTranslation(QString translation) { return "test"; }
In Python I reference the QML method by:
import pyotherside
__translate__ = None
def set_translator(translator):
global __translate__
__translate__ = translator.translate
def get_translation(text):
if __translate__ != None:
return __translate__(text)
else:
return text
In Python I've created a loop that calls this method repeatedly:
def test_function():
while True:
trans = translator.get_translation("Study date")
This will crash almost immediately. I'm not sure this is my root problem but it might be a different problem I've found. If I add some time.sleep() calls or don't call it so much it is fine.
Okay I've finally figured out this issue. Any time I try and access a QObject that's passed into Python I can have it crash. It's not every time though.
I'm currently using QObjects in Python for 2 use cases. The first is the translator functionality above where I was storing a QML function to use in Python. The other case was I was passing a Popup window with a cancel property on it and in Python if the user ever set that boolean property to false I would cancel what I was doing in Python.
I will create a small project that shows it shortly here.
Can you get a backtrace of the crash? Using e.g. gdb or lldb?