Fix memory leak from #12664

Authored by niteria on Oct 6 2016, 7:40 AM.

Description

Fix memory leak from Trac #12664

This fixes the leak with setProgArgv. The problem was
that setProgArgv would not free the objects pointed
to by prog_argc, prog_argv when the globals were
changed resulting in a leak.

The only strictly necessary change is in rts/RtsFlags.c, but
the code in System.Environment was a bit confusing and not
exception safe, so I refactored it.

Test Plan: ./validate

Reviewers: simonmar, ezyang, austin, hvr, bgamari, erikd

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D2576

GHC Trac Issues: Trac #12664