Garry Shandling, a writer and comedian who created and starred in innovative television shows that spoofed his painfully insecure persona and the conventions of television itself, died March 24 at a Los Angeles hospital. He was 66.

Los Angeles police sources told the Associated Press that emergency workers were dispatched to Mr. Shandling’s home for a medical emergency. He was taken to a hospital, where he was pronounced dead. The AP said the cause was a heart attack.

After beginning his career as a writer for sitcoms in the 1970s, Mr. Shandling turned to stand-up comedy and got a major break when he appeared on NBC’s “Tonight Show” with Johnny Carson in 1981. He eventually became a guest host during Carson’s absences and, in the late 1980s, was the show’s permanent guest host.

Mr. Shandling transformed his experiences into two popular series, “It’s Garry Shandling’s Show” and “The Larry Sanders Show,” in which he played versions of himself — a comic actor and talk-show host beset by anxieties, romantic problems and constant fears about his appearance. The shows became models for other comedies set inside show business such as “Curb Your Enthusiasm,” “Entourage,” “30 Rock” and “Episodes.”

“It’s Garry Shandling’s Show,” which aired from 1986 to 1990 on Showtime and later on Fox, was a parody of sitcoms. Playing a character in a sitcom named Garry Shandling, he often addressed the audience directly, breaking the so-called “fourth wall” between performers and audience.

Garry Shandling has died at 66. Here are some of the comedian's most memorable roles and appearances, including his hit HBO series, "The Larry Sanders Show." (The Washington Post)

The practice was used in Marx Brothers films of the 1930s and in TV comedies by George Burns and Jack Benny in the 1950s. But Mr. Shandling approached his camera interactions with a kind of confessional openness that made his audience virtual — and sometimes actual — actors in the show.

“We’re the TV generation,” he told The Washington Post in 1988. “So come on, we know it’s a show, we’re not fooling anybody anymore. So why not do one where you just say, ‘It’s a show, and it’s my life.’ ”

He asked his audiences what they thought of women he was dating; he used stage monitors to spy on the show’s other characters; he beseeched his viewers to laugh more at his jokes. Even the show’s theme song was a self-conscious joke:

“This is the theme to Garry’s show/ The opening theme to Garry’s show/ This is the music that you hear/ As you watch the credits.”

The daringly fresh series “defies comparison with any other program on the air today,” Larry Gelbart, the veteran comedy writer who helped create the series “M.A.S.H.,” wrote in TV Guide. “It is audacious, satirical, hip, sophisticated and wonderfully silly, and, often, miraculously, all of the above at the same time.”

Mr. Shandling later used his experience as a frequent “Tonight Show” host to create a second comedy series, “The Larry Sanders Show,” which ran on HBO from 1992 to 1998. He played a temperamental, egotistical talk-show host named Larry Sanders who required constant reassurance from everyone else at the fictional late-night show.

The rich cast of characters included Mr. Sanders’s deep-voiced but dimwitted sidekick Hank Kingsley, played by Jeffrey Tambor, known for his resounding catchphrase, “Hey, now!” Rip Torn portrayed Mr. Sanders’s unflappable if sometimes smarmy, producer Arthur.

The show also included a parade of celebrities, from Alec Baldwin to Carol Burnett to Robin Williams, who played themselves as guests on “The Larry Sanders Show.” Mr. Shandling showed the stars bickering in dressing rooms, then cursing and snarling during the fictitious show’s commercial breaks, as he transformed glib talk-show comity into gleeful backbiting comedy.

Mr. Shandling shared an Emmy Award for writing in 1998, the show’s final season.

“The Larry Sanders Show” was “all tension, cynicism, profound shallowness, and naughty-boy bonding,” critic Ken Tucker wrote in Entertainment Weekly. “It’s just the way you imagine life behind a big-time TV talk show to be, except infinitely funnier.”

Garry Emmanuel Shandling was born Nov. 29, 1949, in Chicago, where his parents were shopkeepers. The family relocated to the dry climate of Tucson for the their oldest son, Barry, who had cystic fibrosis. His death when Garry was 10 was a traumatic experience, but he described himself as otherwise well adjusted, except for watching “17 hours of TV a day.”

He studied electrical engineering and marketing at the University of Arizona, graduating in 1972. Always drawn to comedy, he showed some of his writing to the already famous comedian George Carlin, who encouraged him to give it a try.

Mr. Shandling moved to Hollywood in 1973 and contributed to such sitcoms as “Sanford and Son,” “Welcome Back, Kotter” and “Three’s Company.”

After a near-fatal car accident in 1977, he decided to focus on stand-up comedy, finally achieving his breakthrough on “The Tonight Show.”

Mr. Shandling was generally the butt of his own humor as he winced at the absurd failures of his hapless love life.

“I’m dating a girl now . . . who’s unaware of it, evidently,” he said.

“I broke up with my girlfriend because she moved in with another guy and, hey, that’s where I draw the line.”

Carson became a particular fan, inviting Mr. Shandling back as a frequent guest and substitute host.

Mr. Shandling made his largest mark on television but also appeared in several films, doing voice work as a pigeon in the Eddie Murphy remake of “Doctor Dolittle” (1988). He had a small role in “Iron Man 2” (2010) and played himself in the 2001 Ben Stiller farce “Zoolander.”

He also co-wrote and starred in “Which Planet Are You From?” (2000), a romantic comedy about an alien, directed by Mike Nichols.

Mr. Shandling kept his personal life largely shielded from view, and a list of survivors could not immediately be confirmed. He was the companion from 1987 to 1994 of Linda Doucett, who played a secretary on “The Larry Sanders Show.”

After the relationship broke up, Mr. Shandling fired her from the show. Doucett filed a wrongful termination and sexual harassment suit that was eventually settled.

Before Carson retired from “The Tonight Show” in 1992, Mr. Shandling had been rumored as a possible successor. By then, however, he had already begun producing and starring in “The Larry Sanders Show.”

Mr. Shandling was offered $5 million to take over NBC’s 12:30 a.m. talk-show slot, but he turned it down.

“I would rather do a series about a talk show,” he said, “than a talk show.”