Copy rbc state more precisely and fix memory management bug.

Resolves #1155. Resolves #1158.

These commits need tests, but they're pretty hard to make.  Pushing off to #1139.