Fixed memleak when writing preferences file