I occasionally find myself passing this or 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
I’m concerned about .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?
Circular references are allowed in VB and in C#. A reference is essentially little more than a value of the size of a machine
WORDin 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.