int dup2(int oldfd, int newfd);

dup2() makes newfd be the copy of oldfd, closing newfd first if necessary, but note the following:

That means newfd (STD*) is closed. The infd, etc, are not closed. Those need to be closed, as they were duped.