Real is in a better situation than Barcelona, and this is because Ronaldo has left Real and Messi hasn't.
Real is trying to build a fresh team based on new young talents. On the other hand, Barcelona is going to over-go a strong crisis by the time Messi resigns or leaves to another club.
Real are not top because Ronaldo is not with them anymore, Infact Real have been struggling ever since Ronaldo left the team, but they are recovering little by little. Barcelona still have their great man Messi, but they have been having managerial issues of late, Valverde not delivering as well as not being the fans favorite, coupled with him being fired, it may take them a while to recover from that. That's why I feel Real may just win the title this season.
If Messi leaves Barcelona, it's going to be a big loss, I don't think they can ever replace him, but he cannot play forever and Barcelona are surely aware of that, so I'm sure they'll have a plan B after Messi leaves.