How do you prevent memory leaks when working with asynchronous tasks or background threads in a mobile app?