QML QObject destroyed before being able to process a signal

1.5k views Asked by At

In QML, I'm using a C++ library that returns a QObject that does a process and emits a signal when is done. In javascript, I use the connect method of the signal being emitted (success) to attach an anonymous function that should handle the signal as the following piece of code shows:

var requestResponse = apiClient.execute(reqInp);
requestResponse.success.connect(function success(response) 
{
        var requestResponseJSON = JSON.parse(response.responseAsJsonString());
        this.append(response.responseAsJsonString());
});

My problem is that sometimes the QML item that contains this method is destroyed before the C++ code being able to complete, so when the signal is emitted, the anonymous function causes an error, because it calls methods that are undefined (in my example, the method append). I have some nasty crashes in iOS and I suspect that this is what might be causing it.

Is there a way of force disconnection of the signal when the object that created the function is destroyed?

1

There are 1 answers

8
synacker On
var requestResponse = apiClient.execute(reqInp);
function myFunction(response) 
{
        var requestResponseJSON = JSON.parse(response.responseAsJsonString());
        this.append(response.responseAsJsonString());
}
requestResponse.success.connect(myFunction);
requestResponse.destroyed.disconnect(myFunction)