Results 1 to 2 of 2

Thread: How can this be nullptr in a member function call?

  1. #1

    Question How can this be nullptr in a member function call?

    I ran into a weird error today which I don't really understand:
    Exception thrown: read access violation. this was nullptr.
    In order to check if the error literally meant that 'this' was nullptr, i added a line above the crashing line which assigned this to a pointer. I added a breakpoint to the line to check, and on the 4th call to the function the variable was null, so clearly 'this' was null.

    Which doesn't make any sense to me. How can a member function be called on an instance that doesn't exist?

    Here's the function declaration/definition:
    Qt Code:
    1. template<class T>
    2. T* GetComponent()
    3. {
    4. static_assert(std::is_base_of<Component, T>::value, "Type parameter 'T' does not derive from Component.");
    6. Entity* th = this; // This variable is null on 4th call.
    7. for (int i = 0; i < m_components.size(); i++) // And so is m_components, so it crashes here.
    8. {
    9. if (typeid(*m_components[i]).hash_code() == typeid(T).hash_code())
    10. {
    11. return dynamic_cast<T*>(m_components[i]);
    12. }
    13. }
    15. XENO_LOG_WARNING("Tried to get a component which doesn't exist on entity.");
    16. return nullptr;
    17. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Munich, Germany
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows

    Default Re: How can this be nullptr in a member function call?

    the calling code is as important (in this case probably more important) than the code you posted.
    This is only the template implementation code.
    If it is null, it means the calling code did not initialized it.
    Please show the surrounding calling code, and the initialization of the template class.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Cannot call member function without object
    By ehntun in forum Qt Programming
    Replies: 0
    Last Post: 24th October 2012, 08:09
  2. How to call member function from int main()
    By GussieBartlett in forum Qt Programming
    Replies: 3
    Last Post: 5th January 2012, 21:06
  3. to call member function
    By vinayaka in forum General Programming
    Replies: 5
    Last Post: 1st July 2011, 14:48
  4. cannot call member function without object
    By been_1990 in forum Qt Programming
    Replies: 11
    Last Post: 23rd October 2010, 18:12
  5. How to call the C++ member function from the JScript
    By parusri in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2008, 11:13

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.