I occasionally find myself passing
Me to a child object. Such as this condensed real-world example...
Public Class Entity Private mValidator as New EntityValidator() Readonly Property IsValid() Return mValidator.Validate(Me) End Property End Class
Estoy preocupado por
.Validate(Me). I get no warnings or Code Analysis violations, but it just feels wrong. I get nervous about infinite recursion and garbage collection.
We have memory leaks on a project and I've wondered if it was due to this. I've never been able to find any resource that addresses the issue.
Is this practice OK, or is my paranoia deserved?
preguntado el 08 de noviembre de 11 a las 19:11
This will not, by itself, cause a memory leak, and there is nothing inherently wrong with passing
this to another object. However, I would be wary of circumstances where the second object actually retains a reference to the passed-in object, as circular references can, in general, cause problems with understandability and maintainability.
Circular references are allowed in VB and in C#. A reference is essentially little more than a value of the size of a machine
WORD in memory. If there's an object A and B, and if A points to B and B points to A, then A internally just holds a value that points to the actual object B and B just has a value that points to the actual object of A.
Someone else explains circular references better than I can, try this thread, about circular references and check the first answer. But in short: no, they won't harm your program and they are allowed and valid, even common.
If you want to find out where your memory leak is coming from, try memory profiling.