Why are my cows/horses despawning in Minecraft PC?

I had 6 cows in a 7x7 pen and 2 blocks deep surrounded by fence at top and now there's three left. I had 4 horses in a 5x5 2 units deep in the ground and there are two left. Why are they despawning?

They don't despawn. No matter how you enclose them, they eventually glitch out and wander away.

It's also possible someone is stealing them.