Usually, when a fully loaded mag won't lock in place with the bolt closed, it's because the top two rounds are subtly jostled out of place. You can test this for yourself using a 30 round MagPul mag. Load it with 30 rounds. Then, while the bolt is closed, insert it into the mag well. It will lock into place. Hit the mag release and reinsert the mag. Do this a few times and suddenly the mag won't lock into place because the top two rounds will shift just a little. Remove the top two rounds and maybe a couple more, then reload them back into the mag. Usually, the mag will once again easily insert and lock. The top rounds often get jostled of place when the mag gets bumped around. For some reason, downloading the mags often keeps the jostled rounds from being a problem.
When loading fresh mags into the AR, use the push/pull method. Push the mag into place, then try to pull it out. Pull hard. If it's not locked in place, it will pull out. Slapping a mag can cause rounds to pop out, or drive the mag too deep into the well