How to prevent this memory leak?

See if one of the following threads suits your case.