Return boolean function c++ Segfault

Why do I keep getting a segfault when all I am trying to do is return a bool variable in a function?

in .h

bool _IsMoving;

in .cpp

bool UBasicNavAnimInstance::GetIsMoving() const
{  
    return _IsMoving;
}

_IsMoving is initialized in the constructor…Moreover the segfault happens within the class itself, not even when trying to access it from another class. If I return true; or false; it works fine as well. I can also read _IsMoving in UBasicNavAnimInstance::NativeUpdateAnimation just fine. I don’t understand…

thanks for your help

Yan

edit: Ah no, my bad it segfaults when I try to access it from another class, still I dont understand why

This is most likely caused by calling GetIsMoving() on a null pointer, which will try and dereference a member variable from inside protected memory, hence the seg fault.

Changing it to return true/false explicitly probably works in that case because the compiler can optimize the dereference away.

I’d double check you definitely have a valid object before calling the function.

you are correct indeed, now why my cast has failed I wonder…

Thanks I will dig deeper