Fix a bug with mallocForeignPtr and finalizers (#10904)

Authored by simonmar.

Description

Fix a bug with mallocForeignPtr and finalizers (Trac #10904)

Summary: See Note [MallocPtr finalizers]

Test Plan: validate; new test T10904

Reviewers: ezyang, bgamari, austin, hvr, rwbarton

Subscribers: thomie

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