Results 1 to 4 of 4

Thread: Referencing Classes ByRef vs ByVal

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Sage SamT's Avatar
    Oct 2006
    Near Columbia

    Referencing Classes ByRef vs ByVal

    Saved From:

    Rob Bruce December 30, 2007 at 11:55 am

    Your Parent properties are causing circular references. These don’t get cleared until the original process is reset. In effect, that’s a memory leak.
    The VB workaround is an absolutely beautiful hack first demonstrated, I believe, by Bruce McKinney. First, change your internal variable for the parent object to a long. This is going to point to the object rather than refer to it. Then use the undocumented objptr and the hack objfromptr functions to convert the object to and from its pointer. This way you’re never storing an object reference, so the circular reference in not created.

    In your child class
     Option Explicit
    'Code edited by SamT:
    '12/03/2017 to remove non-VBA Characters and add """
    Private m_lngParentPtr As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (dest As Any, Source As Any, ByVal bytes As Long)
    The Parent property
    Public Property Get Parent() As Class1
        Set Parent = ObjFromPtr(m_lngParentPtr) 'Returns an object given its pointer.
    End Property
    Public Property Set Parent(obj As Class1)
    m_lngParentPtr = ObjPtr(obj) 'Returns the Pointer of a given Object
    End Property
    This function reverses the effect of the ObjPtr function.
    Private Function ObjFromPtr(ByVal pObj As Long) As Object
        Dim obj As Object
        'force the value of the pointer into the temporary object variable
        CopyMemory obj, pObj, 4
        'assign to the result (this increments the ref counter)
        Set ObjFromPtr = obj
        'manually destroy the temporary object variable
        '(if you omit this step you’ll get a GPF!)
        CopyMemory obj, 0&, 4
    End Function
    Last edited by SamT; 12-03-2017 at 06:35 PM.
    I expect the student to do their homework and find all the errrors I leeve in.

    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts