Ohhh ...
Nicolai, are you running with a client encoding different from server
encoding?
I'm thinking that the real problem is that pg_server_to_client() can
return its input pointer as its result, meaning that CopyAttributeOut
might try to pfree() its input.
regards, tom lane