fix `Call to a member function valid() on a non-object` bug