Open Question: Why does my knee lock up?

Sometimes when I bend my knee in for whatever reason, it locks in place. What happens is I'll bend my knee up (I've noticed it USUALLY happens when I bend in quickly, although since it happens infrequently, I might be wrong), I'll feel this sort of popping sensation in my knee, and then the underside of my knee will feel a little weird (sorry I can't describe it any better than that). Attempting to extend my leg back out is extremely painful. I don't know if just waiting makes it go away or if I'm extending my leg out a certain way. When I manage to extend my leg without pain I'm back to normal until it happens again. I'm not sure what it is, but sometimes it gets really annoying. For example, I fell asleep on the couch, rolled off, woke up and my knee was locked. I was effectively stuck on the floor. My mom and stepfather "helped" me by grabbing my ankle and trying to pull my leg open. Didn't feel too great. Thanks.