0

i have a problem debugging my code. In some Qt Slot i do this:

UIElementInterface* interface = qobject_cast <UIElementInterface*>(sender());

and the following line gives me a segmentation fault:

QStringList data = interface->getSetDefData();

this doesn't

QString hello = interface->getHello();

The implementation of getSetDefData() inside of UIElementInterface:

class UIElementInterface
{
public:
    ...
    UIElementInterface(const QStringList& data) : m_setDefData(data)
    {
        std::cout << qPrintable(m_setDefData[0]) << std::endl; //everything as expected here..
    }

    QStringList getSetDefData(){return m_setDefData;} //gives seg fault
    QString getHello(){return QString("Hello");} //works fine

private:
    QStringList m_setDefData;
};

I would appreciate any help on this one :)

EDIT Based on the comments i used qobject_cast to cast to a CheckBox, which definitely is the sender (in the case of the crash) and It is a subclassed QCheckBox. So it definitely ihnerits QObject (i also double checked the implementation). When I check the return value as suggested it turns out to be 0.

CheckBox* b = qobject_cast<CheckBox*>(sender());
if (b == 0)
{
    std::cout << "Here we are" << std::endl;
}

so that is the problem. I still need to figure out why this is, but this probably causes the crash.

ANOTHER EDIT Printing out the class name sender()->metaObject()->className() exposed the root of evil:

QSignalMapper

Case closed :)

tobilocker
  • 891
  • 8
  • 27
  • 4
    Classical case of crash when `this == NULL`, a NULL (or invalid) pointer object: `getSetDefData()` dereferences `this`, the `getHello()` doesn't. – Dummy00001 Mar 21 '16 at 12:50
  • 1
    The [documentation for `qobject_cast`](http://doc.qt.io/qt-4.8/qobject.html#qobject_cast) clearly says it can return `0` which is the same as a null pointer, something you obviously don't check for. – Some programmer dude Mar 21 '16 at 12:52
  • So if the implementation for `getSetDefData()` would be in a class that inherits `QObject` this would not happen? – tobilocker Mar 21 '16 at 12:56
  • @Dummy00001 could you give some more Information about that please? Calling the constructor does not prevent that object from being NULL? – tobilocker Mar 21 '16 at 13:04
  • @tobilocker, no, you have to check that the pointer returned by `qobject_cast<>()` is not NULL. – Dummy00001 Mar 21 '16 at 13:05
  • I used a static_cast and have the same issue. – tobilocker Mar 21 '16 at 13:06
  • See this: http://stackoverflow.com/questions/669742/accessing-class-members-on-a-null-pointer and http://stackoverflow.com/questions/5431420/why-doesnt-the-program-crash-when-i-call-a-member-function-through-a-null-point – Dummy00001 Mar 21 '16 at 13:07
  • basically the sender probably isnt one of these `UIElementInterface` – AngryDuck Mar 21 '16 at 13:39
  • If you were using a signal mapper, why didn't you use the `QSignalMapper::mapped(QObject *object)` or `QSignalMapper::mapped(QWidget *widget)` signal? – thuga Mar 21 '16 at 15:02

1 Answers1

0

It's clear from the UIElementInterface class declaration that the class is not a QObject. Qt documentation on qobject_cast function states that:

The class T must inherit (directly or indirectly) QObject and be declared with the Q_OBJECT macro.

Thus you cannot use qobject_cast with UIElementInterface class.

vahancho
  • 20,808
  • 3
  • 47
  • 55