简体   繁体   English

QObject 转换上的分段错误

[英]Segmentation Fault on QObject cast

The values are like is not null, canconvert is true and is valid.值就像不为空,canconvert 为真且有效。 But still segmentation fault occurs when trying to cast.但是在尝试投射时仍然会出现分段错误。 I am debugging and the value set might have been deleted just before setValue is called with a new value.我正在调试,并且可能在 setValue 使用新值调用之前删除了值集。

QVariant m_model = 0;

is a member var是成员 var

void Handler::setValue(const QVariant &var)
{
    bool isNull = m_model.isNull();
    bool canConvert = m_model.canConvert<QObject*>();
    bool isValid = m_model.isValid();

    if(!m_model.isNull() && m_model.canConvert<QObject*>()) {
        bool sConverts = (nullptr != m_model.value<QObject*>());
    }

    m_model = var;
}

Can someone tell why it crashes when still m_model has some data in it.有人能说出为什么当 m_model 中仍有一些数据时它会崩溃。

First some problems with your code:首先你的代码有一些问题:

QVariant::canConvert() returns whether the type of the value is convertible at all, not if the actually contained value can be converted. QVariant::canConvert()返回值的类型是否可转换,而不是实际包含的值是否可以转换。 Example: you create a QVariant with a QString as value.示例:您使用 QString 作为值创建 QVariant。 Calling myValue.canConvert<double>() will return true , even if your string was "nonsense".调用myValue.canConvert<double>()将返回true ,即使您的字符串是“废话”。 That's because QVariant is generally able to convert strings like "1.0" to a double value of 1.0.这是因为 QVariant 通常能够将像“1.0”这样的字符串转换为双精度值 1.0。

QVariant::isValid() returns whether the contained type is not QMetaType::UnknownType . QVariant::isValid()返回包含的类型是否不是QMetaType::UnknownType This mostly only happens when you create a QVariant without assigning a value.这通常仅在您创建 QVariant 而不分配值时发生。

QVariant m_model = 0; will create a QVariant with type int.将创建一个类型为 int 的 QVariant。 Don't do that.不要那样做。 Just declare it without the = 0 part.只需在没有= 0部分的情况下声明它。 QVariant (like other value types in Qt, ie QString, QSize etc.) doesn't need to be initialized to be null, empty or invalid. QVariant(与Qt 中的其他值类型,即QString、QSize 等)不需要初始化为null、空或无效。 Declaring it like you did will make isValid() return true , though you might intend it to return false .像您一样声明它会使isValid()返回true ,尽管您可能打算让它返回false

That said, i'm unsure of what you want to achieve in your code snippet.也就是说,我不确定你想在你的代码片段中实现什么。 Basically you're testing m_model before assigning a new untested value var to it (why???).基本上你在为它分配一个新的未经测试的var之前测试m_model (为什么???)。

As you said your value var (or probably the contained value) might have been deleted just before setValue() is called.正如您所说,在调用setValue()之前,您的值var (或可能包含的值)可能已被删除。 If the QObject has been deleted and the pointer has not been set to nullptr explicitly you're handing a QVariant with a dangling pointer to the function.如果 QObject 已被删除并且指针未明确设置为nullptr您正在处理带有悬空指针的 QVariant 函数。 As setValue() doesn't test the new value var you will have that same dangling pointer in m_model , resulting in a crash whenever you try accesing it.由于setValue()不测试新值var您将在m_model拥有相同的悬空指针,因此每当您尝试访问它时都会导致崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM