It sounds like you are experiencing patella alignment problems. This can lead to chodromalacia patella ( wear of the articular cartilage on the back of the knee cap) and in the extreme, recurrent subluxation of the patella. The subluxation would account for the locking, and the feeling that the kneecap is about to "pop out of place".
Your flat feet are a factor, and I would suggest arch supports. Thee can be over the counter at first, and custom orthotics if needed. Finally, physical therapy and bracing should be utilized.
There are surgical procedures that can address this problem, but only if conservative measures fail.
I have already had custom orthotics for a few years. I was in physical therapy and they told me they didn't know what to do and couldn't help me. I have also had more braces than I can count. I went to the orthopedic doctor a year ago and he said, "Hum.. well usually it doesn't last this long. I would suggest you just wait it out." I've been waiting it out for 3 and a half years. I haven't been able to run in that time and was going to join track and field. Do you think it would be safe for me to start running? Thanks for responding!