His family called him unstable and violent, so John R. Houser was ordered by a judge to be taken against his will to a mental hospital in 2008. Despite that sign that Mr. Houser was mentally troubled, he passed a background check and was able to legally purchase the gun he used last week to kill two people in a Louisiana movie theater, because that hospital stay was not defined by officials as an involuntary commitment.

Dylann Roof, who is charged with shooting nine people to death in a Charleston, S.C., church last month, was also able to buy the gun he used in the massacre. He should have failed a background check, federal officials said, because he had previously admitted to illegal drug possession. Yet because of a clerical error in South Carolina and the confusion that caused the federal authorities who examined his record, he was not stopped from his making his purchase.

As these two cases show, the one system that gun rights and gun control advocates both agree on, the National Instant Criminal Background Check System, which is supposed to keep guns out of the hands of dangerous people, is riddled with problems. While the system, in operation since 1998, has prevented more than 2.4 million sales, it still has major gaps, with spotty cooperation from the states and a narrow definition of who is considered too mentally ill to own a gun.

“It works tremendously well when it’s given the tools to work,” said Dan Gross, president of the Brady Campaign to Prevent Gun Violence, an advocacy group. “But it’s widely inconsistent from state to state.”