Only Indirect leaks and no Direct ones
Asked Answered
N

1

5

After reading through this StackOverflow question: What is the difference between a direct and indirect leak? I was left with the impression that if I fix all Direct Leaks (multiple fix-test passes since an indirect leak may become a direct one after fixing the previous direct leaks) eventually I would end up with 0 leaks.

I'm currently using the Leak Sanitizer (LSAN) and after fixing all Direct Leaks (and some indirect leaks were gone as a result) I'm now left with a bunch of Indirect leaks. Why are there no direct ones? When could this happen? How do I diagnose and fix the remaining leaks?

Ng answered 13/8, 2019 at 12:25 Comment(0)
T
6

They might be the circular references. As indirect leak is reachable from other leaked blocks, with cyclic dependency e.g. 2 objects have references to each other, and both of them might be unreachable from the roots.

E.g. in Observer pattern, it's easy to keep circular reference if forget to do a deregistration at the end of usage (Lapsed listener problem).

In general it's better to avoid cyclic references. If think in terms of ownership, owner must have references to objects it owns, but not vise versa, circular dependencies are not possible in this case. It's achievable if pass dependencies via constructor, and not allow to assign dependencies via setters. Also e.g. Rust with borrowing the references makes circular dependencies impossible.

Tobin answered 16/8, 2019 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.