Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

"this" is 0 in debuger



  • I'm trying to find an issue in my code that is strange. Everything seems to work, but when I call a method (set_value) of an object of a custom class it crashes.
    The code looks like this (with a lot of code cut away)

    class MyClass : public QWidget 
    {
    private:
        QString m_name;
    }
    
    void MyClass::set_value(QString key, QString value)
    {
        qDebug()<<value;  // debug is added to this line
        qDebug()<<m_name;  // m_key is a member of the class. 
    

    In the console window in qtcreator I see the value "value" printed, but it crashes when it tries to print m_name.
    I can successfully add

     qDebug()<<m_name;
    

    in the constructor.

    When I try to debug, I add a breakpoint in set_value and it shows in the "Local and Expressions" window:
    this 0x0

    and that can't be good, can it?
    Any suggestions on where to start looking for the problem? It seems counter-intuitive to reach the set_value function if "this" would really be null.


  • Lifetime Qt Champion

    Hi
    Can you show how you use the/a instance of MyClass ?
    Maybe you have an out of scope issues as otherwise it sounds odd.


  • Qt Champions 2017

    @OpenGL said in "this" is 0 in debuger:

    and that can't be good, can it?

    It ain't.

    Any suggestions on where to start looking for the problem? It seems counter-intuitive to reach the set_value function if "this" would really be null.

    Where you call that function with a null pointer - i.e. use the call stack in the debugger and step out of set_value(). There's where you should look for the problem.



  • I found the issue. I was adding the MyClass objects to a Map with the wrong key.
    Hint: never use toInt() without verifying the conversion....


Log in to reply