Avoid memory leak when encoding the key