1

Investigating a crash I found out that a non-QObject derived registered meta type always passes if (obj) checks even if it contains a nullptr.

Outputting the return value says QVariant(Base*) rather than null.

I suspect that QVariant::isNull() might be useful here:

Returns true if this is a null variant, false otherwise. A variant is considered null if it contains a default constructed value or a built-in type instance that has an isNull method, in which case the result would be the same as calling isNull on the wrapped object.

I assume that a nullptr should qualify for a "default constructed value" here, but it doesn't seem to be picked up by conditional statements in QML.

So maybe it is possible to implement that check via a free function? Or maybe I am looking at a bug here?

Update: It was deemed a critical bug.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • Or maybe the `QVariant` is explicitly constructed, given a `nullptr`? You could track back that value to its origin and check where it gets `nullptr` to see why you end up with null. – Pixelchemist Mar 25 '17 at 05:31
  • @Pixelchemist the conversion is implicit, it is converted by the QML engine. The value itself is unusable in QML, as it is neither a Q_OBEJCT nor a Q_GADGET, it is only assigned to pro properties or passed to C++ functions. And yes, the value is definitely a `nullptr` when returned, and even if it passes a check in JS, it is still a `nullptr` when sent back to the C++ side. – dtech Mar 25 '17 at 07:54
  • As of why it is null - that is intended, presupposing that a null pointer would fail a check on the JS side. The problem is that it is not treated as a null in JS. – dtech Mar 25 '17 at 07:55
  • Default-constructed zeros can't make a variant null. The doc probably says that a default-constructed `QVariant` is null. `assert(!QVariant(int{}).isNull());assert(QVariant{}.isNull());` – Velkan Mar 27 '17 at 07:45
  • It says "default constructed value" not "default constructed variant". Default constructed values for Qt containers of type pointers are null pointers. I don't think expecting such a pointer to be evaluated as false in a JS conditional. – dtech Mar 27 '17 at 08:01

0 Answers0