How did you solve the issue?
I don’t see anything that could cause a memory leak.
Since you are storing the mask in each element of the target I would expect an increase in memory usage, but not a leak.