Days before killing his ex-wife inside a Wawa store in Radnor, Brian Kennedy legally purchased an AR-15 rifle, Delaware County District Attorney Katayoun M. Copeland said Thursday.

Kennedy, 34, of Devon, passed an instant background check at a Pennsylvania gun shop on March 16 and walked out with the semiautomatic rifle, the prosecutor said. Twelve days later, he took the gun to the store on Sugartown Road for what was supposed to be a child-custody exchange. Instead, he shot Stephanie Miller, 37, several times in the head and stomach at point-blank range, authorities said. She was pronounced dead at the scene.

Kennedy fled and was found that night in a Glen Mills park, where he overdosed on undisclosed drugs in his car. The couple’s 6-year-old son, who was not present, was unharmed.

Kennedy was arraigned Monday at Crozer-Chester Medical Center on charges of first-degree murder, third-degree murder, and related offenses. After being treated for the overdose, he was released from the hospital on Thursday and taken to the George W. Hill Correctional Facility, Radnor police said.

Kennedy had threatened Miller in the past, including an incident three years earlier at the same store. During a child-custody exchange around 7:30 p.m. on Feb. 15, 2016, Kennedy whispered in Miller’s ear, “Get that [expletive] life insurance policy before I kill you," while she waited in the checkout line, court records show.

He was found guilty of a summary harassment charge in that case, which became the basis for a protection-from-abuse order that lapsed in September. While the order was active, Kennedy would not have been able to legally own weapons. On court forms, neither he nor Miller indicated that he owned any.

After the shooting, authorities did not find any additional weapons in his apartment or his car, Copeland said. The district attorney declined to say at which store he purchased the weapon.

In Pennsylvania, those who have been convicted of serious crimes such as aggravated assault, stalking, rape, and other offenses are prohibited from owning a firearm. People who have been involuntarily committed for mental health treatment or who have an active protection order against them also are restricted.

Before purchasing the gun, Kennedy was approved through the Pennsylvania Instant Check System (PICS), the state’s call center that conducts background checks, often in minutes. When the call center was instituted in 1998, it replaced the mandatory five-day waiting period for gun owners.

Kennedy awaits a preliminary hearing on April 18. No attorney for him was listed on court documents as of Thursday.