Found interesting way to crash QML app.
-
I wanted to provide a function to a function as a parameter. I also wanted the default value to be an empty function. This however will crash if I use parameter=function(){} in the function definition:
import QtQuick 2.15 import QtQuick.Window 2.15 Window { width: 640 height: 480 visible: true title: qsTr("Crash QML Default Function Parameter") // this crashes function crashFunction(functionptr=function(){}){ functionptr(); } // this does not function emptyfunc(){} function nocrashFunction(functionptr=emptyfunc){ functionptr(); } // this is how I fixed it function nocrashFunction(functionptr=null){ if(functionptr){ functionptr(); } } }
I solved this, but I find it interesting that it just kills the application. I would have expected some runtime error of some sort. I suppose this is in a top level function in the app so that exceptions are not caught by the JS engine?
I don't need a fix, I just find this fascinating.
-
Indeed, curious. The crash is in an indirect recursive call to QV4::Compiler::Codegen::defineFunction().
Presumably this means that it is dying in the definition of the default value for
functionptr
. A simplified case triggers the same behavior:import QtQuick 2.15 Item { function crashFunction(functionptr=function(){}){ } }
Storing the function in a property is another workaround.
property var crashFunction: function(functionptr=function(){}){ }
-
I wanted to provide a function to a function as a parameter. I also wanted the default value to be an empty function. This however will crash if I use parameter=function(){} in the function definition:
import QtQuick 2.15 import QtQuick.Window 2.15 Window { width: 640 height: 480 visible: true title: qsTr("Crash QML Default Function Parameter") // this crashes function crashFunction(functionptr=function(){}){ functionptr(); } // this does not function emptyfunc(){} function nocrashFunction(functionptr=emptyfunc){ functionptr(); } // this is how I fixed it function nocrashFunction(functionptr=null){ if(functionptr){ functionptr(); } } }
I solved this, but I find it interesting that it just kills the application. I would have expected some runtime error of some sort. I suppose this is in a top level function in the app so that exceptions are not caught by the JS engine?
I don't need a fix, I just find this fascinating.
@fcarney said in Found interesting way to crash QML app.:
I would have expected some runtime error of some sort.
Indeed; crashing is never OK.
Would you be willing to submit a bug report to https://bugreports.qt.io/ ?
-
@JKSH said in Found interesting way to crash QML app.:
Would you be willing to submit a bug report to https://bugreports.qt.io/ ?
Yeah, I can do that.